csc 331: algorithm analysis decompositions of graphs
TRANSCRIPT
CS
C 3
31
: A
lgori
thm
An
aly
sis
Decompositions of Graphs
A wide range of problems can be expressed with clarity and precision in the concise pictorial
language of graphs.
Consider the task of coloring a political map:
What is the minimum number of colors needed, with the obvious
restriction that neighboring countries should have different
colors?
The precise goal is now to assign a color to each vertex so that no edge has endpoints of the same
color.
2233
4455
66
88
77
99
1313
1111
1212
1010
11
Formally, a graph is specified by a set of vertices V and by edges E between select pairs
of vertices.
V = {1, 2, 3, ..., 13}
2233
4455
66
88
77
99
1313
1111
1212
1010
11E = {{1, 2}, {1, 3},
{1, 4},..., {11, 12}, {11,
13}}
This is an undirected graph.
We can represent a graph by an adjacency matrix:
if there are |V| vertices v1, ..., vn, this is an n × n array whose (i, j)th
entry is
For undirected graphs, the matrix is symmetric since an edge {u, v} can be taken in either
direction.
We can represent a graph by an adjacency list:
It consists of |V| linked lists, one per vertex.
The linked list for vertex u holds the names of vertices to which u has an
outgoing edge.
Adjacency matrix:
-The presence of a particular edge can be checked in constant time.
-The matrix takes up O(V2) space, which is wasteful if the graph does not have very many edges.
Adjacency list:
-The presence of a particular edge can no longer be checked in constant time.
-The total size of the data structure is O(|E|).
CS
C 3
31
: A
lgori
thm
An
aly
sis
Depth-First Search in Undirected Graphs
Chalk prevents looping.
String always takes you back to the starting place, enabling you to return to
passages that you previously saw but did not investigate.
chalk == Boolean variable for each location
string == stack (recursion)
What parts of a graph are reachable from a given vertex?
AA BB EE
II JJGG HH CC FF
KK LL
DD
procedure explore(G, v) Input: G = (V, E) is a graph; v V∈Output: visited(u) is set to true for all nodes u reachable from v
visited(v) = trueprevisit(v)for each edge (v, u) E:∈
if not visited(u): explore(G, u)
postvisit(v)
Starting at A, can we reach J?
AA BB EE
II JJGG HH CC FF
KK LL
DD
Starting at A, can we reach L?
procedure dfs(G)for all v V:∈
visited(v) = false
for all v V:∈if not visited(v):
explore(v)
What is the running time of DFS?
Each vertex is explore’d just once.
During the exploration of a vertex:
Some fixed amount of work (marking the spot as visited and the
pre/postvisit).A loop in which adjacent edges are
scanned, to see if they lead somewhere new.
What is the running time of DFS?
This loop takes a different amount of time for each vertex, so let’s consider all vertices
together.Some fixed amount of work (marking
the spot as visited and the pre/postvisit).
A loop in which adjacent edges are scanned, to see if they lead somewhere
new.
O(|V|) - Constant amount of work per vertex
O(|E|) - Each edge {x, y} ∈ E is examined exactly twice.
What is the running time of DFS?
O(|V| + |E|)
Linear in the size of its input.
CS
C 3
31
: A
lgori
thm
An
aly
sis
Connectivity in Undirected Graphs
An undirected graph is connected if there is a path between any pair of vertices.
AA BB EE
II JJGG HH CC FF
KK LL
DD
What additional information would DFS give us if we defined pre/postvisit as
follows?
procedure previsit(v)pre[v] = clockclock = clock + 1
procedure postvisit(v)post[v] = clockclock = clock + 1
CS
C 3
31
: A
lgori
thm
An
aly
sis
Decompositions of Directed Graphs
DD
GG HH
AA CCBB
FFEE
Our DFS algorithm can be run verbatim on directed graphs, taking care to
traverse edges only in their prescribed directions.
DD
GG HH
AA CCBB
FFEE
AA 1
BB 2
EE 3
FF 4
GG 5,6
,7 HH 8,9
,10
,11 CC 12
DD 13,14
,15
,16
DD
GG HH
AA CCBB
FFEE
AA 1
BB 2
EE 3
FF 4
GG 5,6
,7 HH 8,9
,10
,11 CC 12
DD 13,14
,15
,16Other edges are either Back, Forward, or Cross edges:
F
F
BB
C
C
Other edges (u,v) are either Back, Forward, or Cross edges:
Forward edge: pre(u) < pre(v) < post(v) < post(u)(Same as tree edge, but unvisited)
Back edge: pre(v) < pre(u) < post(u) < post(v)
Cross edge: pre(v) < post(v) < pre(u) < post(u)
A cycle in a directed graph is a circular path.
v0 → v1 → v2 → … → vk → v0
A graph without cycles is acyclic.What algorithm tells if a graph is acyclic?
DD
GG HH
AA CCBB
FFEE
AA CC
DD
Direct acyclic graphs (dags) are quite common, especially in modeling
relationships.
For DAGs, we often want to order the nodes linearly (topological sort). All edges are
forward edges.
DD
GG HH
AA CCBB
FFEE
BBAA CC EEDD FF GGHH
What algorithm will give a topological sort?What is the running time?