Anda di halaman 1dari 10

Data Structures and Algorithms

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. 

Breadth­First Search – If a graph is undirected, we represent an edge 

Depth­First 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 

DAS09 M. Böhlen 3 DAS09 M. Böhlen 4


Graph Terminology/2 Graph Terminology/3

Simple path – a path with no repeated  ●
Subgraph – a subset of vertices and 
vertices edges forming a graph

Cycle – a simple path, except  a b

Connected component – maximal  
that the last vertex is the same  c
as the first vertex d e
connected subgraph. 

Connected graph: any two vertices are 
– For example, the graph below has 3 
connected by some path connected components

DAS09 M. Böhlen 5 DAS09 M. Böhlen 6

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
– Maze­solving
– Networks: routing, searching, clustering, etc.

DAS09 M. Böhlen 11 DAS09 M. Böhlen 12


Breadth First Search/1 Breadth­First Search/2

A Breadth­First Search (BFS) starts at vertex s  ●
In the second round, all the new edges that 
and visits all vertexes reachable from s. In doing  can be reached by unrolling the string 2 edges 
so BFS defines a spanning tree. are visited and assigned a distance of 2

BFS in a graph G is like wandering in a labyrinth  ●
This continues until every vertex has been 
with a string and exploring the entire neighbor­ assigned a level
hood first. ●
The label of any vertex v corresponds to the 

The starting vertex s is assigned distance 0. length of the shortest path (in terms of edges) 

In the first round the string is unrolled 1 unit. All  from s to v
edges that are 1 edge away from the anchor are 
visited (discovered) and assigned distance 1.
DAS09 M. Böhlen 13 DAS09 M. Böhlen 14

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
Depth­First Search/1

Given a graph G = (V,E), BFS discovers all  ●
A depth­first 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 breadth­first 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

Depth­First 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 DFS­All 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
depth­first 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 DFS­All 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 DFS­All 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 well­formed 
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)

DAS09 M. Böhlen 27 DAS09 M. Böhlen 28


DFS Edge Classification/1 DFS Edge Classification/2 13


Tree edge (gray to white)  ●
Forward edge (gray to black) 
– Edges in depth­first forest – Nontree edge from ancestor to descendant 

Back edge (gray to gray) in depth­first tree
– from descendant to ancestor in depth­first 

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
– Self­loops 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.

DAS09 M. Böhlen 31 DAS09 M. Böhlen 32


DAG Theorem 10
Topological Sort Example

A directed graph G is acyclic if and only if a DFS  ●
Precedence relations: an edge from x to y means 
of G yields no back edges. Proof: one must be done with x before one can do y
– suppose there is a back edge (u,v); v is an ancestor  ●
Intuition: can schedule task only when all of its 
of u in DFS forest. Thus, there is a path from v to u in 
G and (u,v) completes the cycle subtasks have been scheduled 
– suppose there is a cycle c; let v be the first vertex in  1/8 shirt 11/16 undershorts socks 17/18
watch 9/10
c to be discovered and u is a predecessor of v in c. 
2/5 tie 12/15 pants shoes 13/14

Upon discovering v the whole cycle from v to u is white

We visit all nodes reachable on this white path before  3/4 jacket belt 6/7
DFS(v) returns, i.e., vertex u becomes a descendant of v

Thus, (u,v) is a back edge
socks undershorts pants shoes watch shirt belt tie jacket

Thus, we can verify a DAG using DFS. 17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4

DAS09 M. Böhlen 33 DAS09 M. Böhlen 34

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

DAS09 M. Böhlen 35 DAS09 M. Böhlen 36


Topological Sort Correctness Topological Sort Running Time 14
15


Claim: DAG & (u,v)∈E => u.endtime>v.endtime ●
Running time

When (u,v) explored, u is gray. We can  – depth­first 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.

DAS09 M. Böhlen 37 DAS09 M. Böhlen 38

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
– Breadth­First Search (BFS) – Shortest Paths
– Depth­First Search (DFS) ●
Dijkstra’s algorithm

Bellman­Ford algorithm

Topological sort

DAS09 M. Böhlen 39 DAS09 M. Böhlen 40

Anda mungkin juga menyukai