Week 9 Graphs – Definition
●
A graph G = (V,E) is composed of:
1. Graphs – Definitions – V: set of vertices
– E⊂ V× V: set of edges connecting vertices
2. Graph Representations
●
An edge e = (u,v) is a pair of vertices
3. Traversing Graphs ●
We assume directed graphs.
●
BreadthFirst Search – If a graph is undirected, we represent an edge
●
DepthFirst Search between u and v by having (u,v) ∈ E and (v,u)
∈ E V = {A, B, C, D}
A A
4. DFS Annotations B B
E = {(A,B), (B,A), (A,C), (C,A),
5. Distributed Acyclic Graphs C D C D
(C,D), (D,C), (B,C), (C,B)}
DAS09 M. Böhlen 2
Applications Graph Terminology/1
●
Electronic circuits, pipeline networks ●
Vertex v is adjacent to vertex u iff (u,v) ∈ E
●
Transportation and communication ●
degree of a vertex: # of adjacent vertices
networks 3 2
●
Modeling any sort of relationtionships
(between components, people, 3
processes, concepts) 3 3
● Path – a sequence of vertices v1 ,v2 ,. . .vk such
that vi+1 is adjacent to vi for i = 1 .. k – 1
Graph Terminology/4 Data Structures for Graphs 2
●
tree – connected graph without cycles ●
The adjacency list of a vertex v: a sequence
●
forest – collection of trees of vertexes adjacent to v
●
Represent the graph by a b c d
the adjacency lists of all b a e
its vertexes
c a d e
a b
d a c e
c
d e e b c d
Space = Θ( V + ∑ deg(v)) = Θ( V + E )
DAS09 M. Böhlen 7 DAS09 M. Böhlen 8
Adjacency Matrix 1
Pseudocode Assumptions
●
Matrix M with entries for all pairs of vertices ●
Each node has some properties (fields of a
●
M[i,j] = true iff there is an edge (i,j) in the record):
graph
– adj: list of adjacent nodes
– dist: distance from start node in a traversal
●
M[i,j] = false iff there is no edge (i,j) in the – pred: predecessor in a traversal
graph – color: color of the node (is changed during
●
Space = O(|V|2) traversal; white, gray, black)
A B C D E
a b A F T T T F – starttime: time when first visited during a
B T F F F T traversal (depth first search)
c C T F F T T – endtime: time when last visited during a traversal
D T F T F T
d e E F T T T F
(depth first search)
DAS09 M. Böhlen 9 DAS09 M. Böhlen 10
Graph Searching Algorithms Coloring of Vertexes 8
5
●
Systematic search of every edge and vertex of ●
A vertex is white if it is undiscovered
the graph ●
A vertex is gray if it has been discovered but
●
Graph G = (V,E) is either directed or not all of its edges have been explored
undirected ●
A vertex is black after all of its adjacent
●
Applications vertices have been discovered (the adj. list
was examined completely)
– Compilers
– Graphics
– Mazesolving
– Networks: routing, searching, clustering, etc.
BFS Algorithm BFS Running Time
12
5
9
23
BFS(G,s)
for u∈G.V do
u.color := white
●
Given a graph G = (V,E)
u.dist := ∞ Init all vertices – Vertices are enqueued if their color is white
u.pred := NIL
s.color := gray
– Assuming that en and dequeuing takes O(1) time
s.dist := 0 the total cost of this operation is O(V)
init-queue(Q) // FIFO queue Init BFS with s
enqueue(Q,s)
– Adjacency list of a vertex is scanned when the vertex
while not isEmpty(Q) do is dequeued
u := head(Q)
for v∈u.adj do Handle all of u's – The sum of the lengths of all lists is (E).
if v.color = white then children before Thus, O(E) time is spent on scanning them.
v.color := gray
v.dist := u.dist + 1;
handling children – Initializing the algorithm takes O(V)
v.pred := u; of children
enqueue(Q,v)
●
Total running time O(V+E) (linear in the size
dequeue(Q) of the adjacency list representation of G)
u.color := black
DAS09 M. Böhlen 15 DAS09 M. Böhlen 16
BFS Properties 20
DepthFirst Search/1
●
Given a graph G = (V,E), BFS discovers all ●
A depthfirst search (DFS) in a graph G is
vertices reachable from a source vertex s. like wandering in a labyrinth with a string
and following one path to the end
●
It computes the shortest distance to all
– We start at vertex s, tying the end of our string to
reachable vertices. s and painting s “visited (discovered)”. We label s
●
It computes a breadthfirst tree that contains as our current vertex called u.
all such reachable vertices. – Now, we travel along an arbitrary edge (u,v).
– If edge (u,v) leads us to an already visited vertex v
●
For any vertex v reachable from s, the path in
we return to u.
the breadth first tree from s to v, corresponds – If vertex v is unvisited, we unroll our string, move
to a shortest path in G. to v, paint v “visited”, set v as our current vertex,
and repeat the previous steps.
DAS09 M. Böhlen 17 DAS09 M. Böhlen 18
DepthFirst Search/2 DFS Algorithm/1 6
DFS-All(G)
●
Eventually, we will get to a point where all for u∈G.V do
u.color := white
edges from u lead to visited vertices u.pred := NIL Init all vertexes
time := 0
●
We then backtrack by rolling up our string for u∈G.V do Visit all vertexes
until we get back to a previously visited if u.color = white then DFS(u)
vertex v. DFS(u)
u.color := gray
●
v becomes our current vertex and we repeat time := time + 1
the previous steps u.starttime := time
for v∈u.adj do
Visit all children
if v.color = white then recursively (children
v.pred := u; of children are visited
DFS(v)
u.color := black first)
time := time + 1
u.endtime := time
DAS09 M. Böhlen 19 DAS09 M. Böhlen 20
DFS Algorithm/2 DFS Algorithm Running Time 7
●
Initialize – color all vertices white ●
Running time
●
Visit each and every white vertex using DFS – the loops in DFSAll take time Θ (V) each,
excluding the time to execute DFS
All (even if there are disconnected trees). – DFS is called once for every vertex
●
Each call to DFS(u) roots a new tree of the ●
its only invoked on white vertices, and
depthfirst forest at vertex u ●
paints the vertex gray immediately
– for each DFS a loop interates over all v.adj
●
When DFS returns, each vertex u has assigned
– a discovery time d[u] ∑v ∈V
∣v.adj∣= E
– a finishing time f[u] – the total cost for DFS is (E)
– the running time of DFSAll is (V+E)
DAS09 M. Böhlen 21 DAS09 M. Böhlen 22
DFS versus BFS Generic Graph Search
●
The BFS algorithms visits all vertices that GenericGraphSearch(G,s)
01 for each vertex u ∈ G.V { u.color := white; u.pred := NIL }
are reachable from the start vertex. It 04 s.color := gray
05 init(GrayVertices)
06 add(GrayVertices,s)
returns one search tree. 07 while not isEmpty(GrayVertices)
08 u := remove(GrayVertices)
●
The DFSAll algorithm visits all vertices in 09 for each v ∈ u.adj do
10 if v.color = white then
the graph. It may return multiple search 11 v.color := gray
12 v.pred := u
trees. 13 add(GrayVertices,v)
14 u.color := black
●
The difference comes from the applications
of BFS and DFS. This behavior of the algo ● BFS if GrayVertices is a Queue (FIFO)
rithms can be changed. ● DFS if GrayVertices is a Stack (LIFO)
DAS09 M. Böhlen 23 DAS09 M. Böhlen 24
DFS Annotations DFS Timestamping
●
A DFS can be used to annotate vertices ●
Vertex u is
and edges with additional information. – white before u.starttime
– starttime (when was the vertex visited first) – gray between u.starttime and u.endtime, and
– endtime (when was the vertex visited last) – black after u.endtime
– edge classification (tree, forward, back or ●
Notice the structure througout the algorithm
cross edge) – gray vertices form a linear chain
●
The annotations reveal useful information – correponds to a stack of vertices that have not
about the graph that is used by advanced been exhaustively explored (DFS started but not
yet finished)
algorithms.
DAS09 M. Böhlen 25 DAS09 M. Böhlen 26
DFS Parenthesis Theorem/1 DFS Parenthesis Theorem/2
y z s t
●
Start and end times have parenthesis structure 3/6 2/9 1/10 11/16
– represent starttime of u with left parenthesis "(u" B F C B
– represent endtime of u with right parenthesis "u)" 4/5 7/8 12/13 14/15
C C C
x w v u
– history of start and endtimes makes a wellformed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
expression (parenthesis are properly nested)
s t
●
Intuition for proof: any two intervals are either
z v u
disjoint or enclosed
– Overlaping intervals would mean finishing ancestor, y w
before finishing descendant or starting descendant x
without starting ancestor (s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t)
●
Tree edge (gray to white) ●
Forward edge (gray to black)
– Edges in depthfirst forest – Nontree edge from ancestor to descendant
●
Back edge (gray to gray) in depthfirst tree
– from descendant to ancestor in depthfirst
●
Cross edge (gray to black)
tree y z s t – remainder – between trees or subtrees
3/6 2/9 1/10 11/16 y z s t
– Selfloops 3/6 2/9 1/10 11/16
B F C B
B F C B
4/5 7/8 12/13 14/15
C C C
x w v u 4/5 7/8 12/13 14/15
C C C
x w v u
DAS09 M. Böhlen 29 DAS09 M. Böhlen 30
DFS Edge Classification/3 Directed Acyclic Graphs
●
In a DFS the color of the next vertex ●
A DAG is a directed graph without cycles.
decides the edge type (this makes it
impossible to distinguish forward and
cross edges).
●
Tree and back edges are important. ●
DAGs are used to indicate precedence among
●
Most algorithms do not distinguish events (event x must happen before y).
between forward and cross edges. ●
An example would be a parallel code execution.
●
We get total order using Topological Sorting.
Topological Sort/1 16
Topological Sort/2
●
Sorting of a directed acyclic graph (DAG). ●
The following algorithm topologically sorts a
●
A topological sort of a DAG is a linear DAG.
ordering of all its vertices such that for any ●
The linked lists comprises a total ordering.
edge (u,v) in the DAG, u appears before v in
the ordering. TopologicalSort(G)
Call DSF(G) to compute v.endtime for
each vertex v
As each vertex is finished, insert it
at the beginning of a linked list
Return the linked list of vertices
●
Claim: DAG & (u,v)∈E => u.endtime>v.endtime ●
Running time
●
When (u,v) explored, u is gray. We can – depthfirst search: O(V+E) time
distinguish three cases:
– v = gray ==> (u,v) = back edge (cycle, contradiction)
– insert each of the |V| vertices to the front
– v = white ==> v becomes descendant of u of the linked list: O(1) per insertion
==> v will be finished before u
==> v.endtime < u.endtime ●
Thus the total running time is O(V+E).
– v = black ==> v is already finished
==> v.endtime < u.endtime
●
The definition of topological sort is satisfied.
Summary Next Week
●
Graphs ●
Graphs:
– G = (V,E), vertex, edge, (un)directed – Weighted graphs
graph, cycle, connected component, ... – Minimum Spanning Trees
●
Graph representation: adjanceny list/matrix ●
Kruskal’s algorithm
●
Basic techniques to traverse/search graphs ●
Prim’s algorithm
– BreadthFirst Search (BFS) – Shortest Paths
– DepthFirst Search (DFS) ●
Dijkstra’s algorithm
●
BellmanFord algorithm
●
Topological sort