Vous êtes sur la page 1sur 33

3/2/13

Algorithms

Module 7: Graphs, Part I


Module objectives

By the end of this module, you will be able to: Explain what a graph is. Estimate, in order-notation, the number of edges of a fully-connected graph. Explain the benefits of using the two data structures (adjacency-matrix, adjacency-list) to represent graphs. Demonstrate how breadth-first search works. Demonstrate how depth-first search works. Define a strongly-connected component in a directed graph. Explain the key ideas in one of the strong-component algorithms for directed graphs. Demonstrate how to compute a topological sort of a directed acyclic graph.

What is a Graph?

Informal definition: A graph is a mathematical abstraction used to represent "connectivity information". A graph consists of vertices and edges that connect them, e.g.,

It shouldn't be confused with the "bar-chart" or "curve" type of graph.

Formally: A graph G = (V, E) is: a set of vertices V and a set of edges E = { (u, v): u and v are vertices }. Two types of graphs: Undirected graphs : the edges have no direction. Directed graphs : the edges have direction. Example: undirected graph

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

1/33

3/2/13

Algorithms

Edges have no direction. If an edge connects vertices 1 and 2, either convention can be used: No duplication: only one of (1, 2) or (2, 1) is allowed in E. Full duplication: both (1, 2) and (2, 1) should be in E. Example: directed graph

Edges have direction (shown by arrows). The edge (3, 6) is not the same as the edge (6, 3) (both exist above).

Depicting a graph: The picture with circles (vertices) and lines (edges) is only a depiction => a graph is purely a mathematical abstraction. Vertex labels: Can use letters, numbers or anything else. Convention: use integers starting from 0. => useful in programming, e.g. d e g r e e [ i ]= degree of vertex i. Edges can be drawn "straight" or "curved". The geometry of drawing has no particular meaning:

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

2/33

3/2/13

Algorithms

Graph conventions: What's allowed (but unusual) in graphs: Self-loops (occasionally used). Multiple edges between a pair of vertices (rare). Disconnected pieces (frequent in some applications). Example:

What's not (conventionally) allowed: Mixing undirected and directed edges. Re-using labels in vertices. Bidirectional arrows. Most common: No multiple edges. No self-loops. Other terms used: Vertices: nodes, terminals, endpoints. Edges: links, arcs.

In-Class Exercise 7.1: If we disallow multiple edges and self-loops, what is the maximum number of edges in an undirected graph with n vertices? What is this number in order-notation?

Definitions: Degrees : Undirected graph: the degree of a vertex is the number of edges incident to it. Directed graph: the out-degree is the number of (directed) edges leading out, and the in-degree is the number of (directed) edges terminating at the vertex.
www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 3/33

3/2/13

Algorithms

Neighbors : Two vertices are neighbors (or are adjacent) if there's an edge between them. Two edges are neighbors (or are adjacent) if they share a vertex as an endpoint. Paths : Undirected: a sequence of vertices in which successive vertices are adjacent. Directed: a sequence of vertices in which every pair of successive vertices has this property: there's a directed edge from the first to the second. A simple path does not repeat any vertices (and therefore edges) in the sequence. A cycle is a simple path with the same vertex as the first and last vertex in the sequence. Connectivity: Undirected: Two vertices are connected if there is a path that includes them. Directed: Two vertices are strongly-connected if there is a (directed) path from one to the other. Components: A subgraph is a subset of vertices together with the edges from the original graph that connects vertices in the subset. Undirected: A connected component is a subgraph in which every pair of vertices is connected. Directed: A strongly-connected component is a subgraph in which every pair of vertices is strongly-connected. A maximal component is a connected component that is not a proper subset of another connected component. Digraph: another name for a directed graph. Example:

More definitions: Euler tour: A cycle that traverses all edges exactly once (but may repeat vertices).

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

4/33

3/2/13

Algorithms

Known result: Euler tour exists if and only if all vertices have even degree. Hamiltonian tour: A cycle that traverses all vertices exactly once.

Known result: testing existence of a Hamiltonian tour is (very) difficult. Euler path: A path that traverses all edges exactly once. Hamiltonian path: A path that traverses all vertices exactly once. Trees : A tree is a connected graph with no cycles. A spanning tree of a graph is a connected subgraph that is a tree.

Weighted graphs : Sometimes, we include a "weight" (number) with each edge. Weight can signify length (for a geometric application) or "importance". Example:

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

5/33

3/2/13

Algorithms

Why are graphs important? History: Maze-searching. Euler's crossing problem: the Konigsberg bridges

How to cross each bridge just once and return to starting point? Applications: Fundamental mathematical construct to represent "connectivity". Appears in thousands of problems. Source of many classic problems: traveling salesman, routing, spanning trees. Many "graph-structured" applications: networks, transportation-systems, electronic circuits, molecules. Source of theory: Many important algorithms. Key to understanding algorithm design and analysis. Simple to describe, yet perplexing: Euler tour: easy problem. Hamiltonian tour: hard problem.

The field of graph theory: Large area of mathematics: Analysis of general graphs. Analysis of special types of graphs. Many classic problems e.g., the four-color theorem. Optimization problems based on graphs, e.g., shortest-paths. Graph algorithms: an area in computer science. Rich source of algorithms, theory, insight. Useful algorithms used in many applications (e.g., in a compiler).
www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 6/33

3/2/13

Algorithms

In this course: Fundamental algorithms for exploring a graph: breadth-first and depth-first. Finding shortest paths and minimum spanning tree. Convention about undirected vs. directed: When not specified, assume undirected. Unless otherwise mentioned, an algorithm or definition about undirected graphs usually can be modified to apply to directed graphs. (although, directed graphs are usually more complicated).

In-Class Exercise 7.2: Suppose di is the degree of vertex i in a connected undirected graph with n vertices and m edges. Let D = d1 + ... + dn . What is the relation between D and m?

Graph Data Structures

First, an idea that doesn't work: We have already represented trees (like binary trees) with node instances and pointers between instances. Idea: use a node instance for each vertex, and a pointer from one vertex to another if an edge exists between them. In-Class Exercise 7.3: Draw an example graph and the corresponding pointer-based data structure. Why doesn't it work? The two fundamental data structures: Adjacency matrix. Key idea: use a 2D matrix. Row i has "neighbor" information about vertex i. Undirected: a d j M a t r i x [ i ] [ j ] = 1if and only if there's an edge between vertices i and j . a d j M a t r i x [ i ] [ j ] = 0otherwise. Directed: a d j M a t r i x [ i ] [ j ] = 1if and only if there's an edge from i to j . a d j M a t r i x [ i ] [ j ] = 0otherwise. Example: undirected

01100000 10100000 11010100 00101010 00010010 00100011 00011100 00000100


www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 7/33

3/2/13

Algorithms

Note: a d j M a t r i x [ i ] [ j ]= =a d j M a t r i x [ j ] [ i ](convention for undirected graphs). Example: directed

01000000 00100000 10010100 00001010 00000010 00000001 00010000 00000000

Adjacency list. Key idea: use an array of vertex-lists. Each vertex list is a list of neighbors. Example: undirected

Example: directed

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

8/33

3/2/13

Algorithms

Convention: in each list, keep vertices in order of insertion => add to rear of list Both representations allow complete construction of the graph. Advantages of matrix: Simple to program. Some matrix operations (multiplication) are useful in some applications (connectivity). Efficient for dense (lots of edges) graphs. Advantages of adjacency list: Less storage for sparse (few edges) graphs. Easy to store additional information in the data structure. (e.g., vertex degree, edge weight)

In-Class Exercise 7.4: Suppose a graph has V vertices and E edges. In order-notation (in terms of V and E), what is the size of the adjacency matrix representation? What is the size of the adjacency list representation?

In-Class Exercise 7.5: Write a Java class to implement the adjacency list representation. Do not write all the details, but merely what you need for initializing the data structure.

Graph ADT: ADT = Abstract Data Type. What operations should a graph ADT support? Insertion of vertices, edges. Is the graph connected? What are the connected components? Does the graph have a cycle? Example: consider this interface for an undirected graph ADT:
p u b l i ci n t e r f a c eU n d i r e c t e d G r a p h S e a r c h A l g o r i t h m{ / /C a l lt h i so n c et os e tu pd a t as t r u c t u r e s . p u b l i cv o i di n i t i a l i z e( i n tn u m V e r t i c e s ,b o o l e a ni s W e i g h t e d ) ;
www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 9/33

3/2/13

Algorithms

/ /C a l lt h i sr e p e a t e d l yw i t he a c he d g et ob ei n s e r t e d : p u b l i cv o i di n s e r t U n d i r e c t e d E d g e( i n ts t a r t V e r t e x ,i n te n d V e r t e x ,d o u b l ew e i g h t ) ;

/ /R e t u r nt h en u m b e ro fc o n n e c t e dc o m p o n e n t s .I f" 1 " ,t h e nt h ew h o l e / /g r a p hi sc o n n e c t e d . p u b l i ci n tn u m C o n n e c t e d C o m p o n e n t s( ) ;

/ /R e t u r nt h ec o m p o n e n tt ow h i c he a c hv e r t e xb e l o n g s . p u b l i ci n t [ ]c o m p o n e n t L a b e l s( ) ;

/ /I st h e r eac y c l ei nt h eg r a p h ? p u b l i cb o o l e a ne x i s t s C y c l e ( ) ; }

In-Class Exercise 7.6: How would you use this ADT to tell whether a graph is a connected tree?

Next, let's look at some sample code using the matrix representation: Convention for weighted graphs: use weight=0 to represent the lack of an edge.
p u b l i cc l a s sU n d i r e c t e d G r a p h S e a r c hi m p l e m e n t sU n d i r e c t e d G r a p h S e a r c h A l g o r i t h m{ i n tn u m V e r t i c e s ; i n tn u m E d g e s ; b o o l e a ni s W e i g h t e d ; d o u b l e[ ] [ ]a d j M a t r i x ; / /N u m b e ro fv e r t i c e s ,g i v e na si n p u t . / /W ek e e pt r a c ko ft h en u m b e ro fe d g e s . / /I st h i saw e i g h t e dg r a p h ? / /T h em a t r i x .N o t e :w eu s e" d o u b l e "t os t o r e / /" d o u b l e "w e i g h t s ,i ft h eg r a p hi sw e i g h t e d .

p u b l i cv o i di n i t i a l i z e( i n tn u m V e r t i c e s ,b o o l e a ni s W e i g h t e d ) { / /S t o r e : t h i s . n u m V e r t i c e s=n u m V e r t i c e s ; t h i s . i s W e i g h t e d=i s W e i g h t e d ; / /C r e a t et h ea d j a c e n c ym a t r i x . a d j M a t r i x=n e wd o u b l e[ n u m V e r t i c e s ] [ ] ; f o r( i n ti = 0 ;i<n u m V e r t i c e s ;i + + ){ a d j M a t r i x [ i ]=n e wd o u b l e[ n u m V e r t i c e s ] ; f o r( i n tj = 0 ;j<n u m V e r t i c e s ;j + + ) a d j M a t r i x [ i ] [ j ]=0 ; n u m E d g e s=0 ; }

/ /I n s e r tag i v e ni n p u te d g e . p u b l i cv o i di n s e r t U n d i r e c t e d E d g e( i n ts t a r t V e r t e x ,i n te n d V e r t e x ,d o u b l ew e i g h t ) { / /U n w e i g h t e dg r a p h :u s ew e i g h t1 . 0 . i f( !i s W e i g h t e d ){ w e i g h t=1 . 0 ; } / /I n s e r ti nb o t hp l a c e s : a d j M a t r i x [ s t a r t V e r t e x ] [ e n d V e r t e x ]=w e i g h t ;


www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 10/33

3/2/13

Algorithms

a d j M a t r i x [ e n d V e r t e x ] [ s t a r t V e r t e x ]=w e i g h t ; n u m E d g e s+ + ; }

The same with an adjacency list:


p u b l i cc l a s sU n d i r e c t e d G r a p h S e a r c hi m p l e m e n t sU n d i r e c t e d G r a p h S e a r c h A l g o r i t h m{ i n tn u m V e r t i c e s ; i n tn u m E d g e s ; b o o l e a ni s W e i g h t e d ; L i n k e d L i s t [ ]a d j L i s t ; / /N u m b e ro fv e r t i c e s ,g i v e na si n p u t . / /W ek e e pt r a c ko ft h en u m b e ro fe d g e s . / /I st h i saw e i g h t e dg r a p h ? / /T h el i s t( o fl i s t s ) :o n el i s tf o re a c hv e r t e x . / /W ea r eu s i n gj a v a . u t i l . L i n k e d L i s ta so u rl i n k e dl i s t . / /T h i sw i l ls t o r ei n s t a n c e so fG r a p h E d g e .

/ /W ew i l lt e s tn e wi n s e r t i o n st os e ei ft h e ye x i s t .B e c a u s eL i n k e d L i s tp e r f o r m s / /" . e q u a l s ( ) "t e s t i n gf o rc o n t a i n m e n t ,w en e e da ni n s t a n c e( a n do n l yo n e )o fG r a p h E d g e / /f o rs u c ht e s t i n g . G r a p h E d g et e s t E d g e=n e wG r a p h E d g e( 1 ,1 ,0 ) ;

p u b l i cv o i di n i t i a l i z e( i n tn u m V e r t i c e s ,b o o l e a ni s W e i g h t e d ) { / /S t o r e : t h i s . n u m V e r t i c e s=n u m V e r t i c e s ; t h i s . i s W e i g h t e d=i s W e i g h t e d ; / /A d j a c e n c y l i s tr e p r e s e n t a t i o n a d j L i s t=n e wL i n k e d L i s t[ n u m V e r t i c e s ] ; f o r( i n ti = 0 ;i<n u m V e r t i c e s ;i + + ) a d j L i s t [ i ]=n e wL i n k e d L i s t( ) ; / /N o t e :w ea r eu s i n gj a v a . u t i l . L i n k e d L i s t . n u m E d g e s=0 ; }

/ /I n s e r tag i v e ni n p u te d g e . p u b l i cv o i di n s e r t U n d i r e c t e d E d g e( i n ts t a r t V e r t e x ,i n te n d V e r t e x ,d o u b l ew e i g h t ) { i f( !i s W e i g h t e d ){ w e i g h t=1 . 0 ; } / /A d j l i s tr e p r e s e n t a t i o n :s e ei ft h ee d g ei sa l r e a d yt h e r e . t e s t E d g e . s t a r t V e r t e x=s t a r t V e r t e x ; t e s t E d g e . e n d V e r t e x=e n d V e r t e x ; / /E x p l o i tt h em e t h o d si nj a v a . u t i l . L i n k e d L i s t . i f( a d j L i s t [ s t a r t V e r t e x ] . c o n t a i n s( t e s t E d g e ) ){ / /. . .r e p o r te r r o r. . . r e t u r n ; } / /I t ' su n d i r e c t e d ,s oa d dt ob o t hv e r t e xl i s t s . G r a p h E d g ee=n e wG r a p h E d g e( s t a r t V e r t e x ,e n d V e r t e x ,1 . 0 ) ; a d j L i s t [ s t a r t V e r t e x ] . a d d L a s t( e ) ; / /W ew o u l d n ' th a v et h i sf o rd i r e c t e dg r a p h s : G r a p h E d g ee 2=n e wG r a p h E d g e( e n d V e r t e x ,s t a r t V e r t e x ,1 . 0 ) ; a d j L i s t [ e n d V e r t e x ] . a d d L a s t( e 2 ) ; n u m E d g e s+ + ; }


www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 11/33

3/2/13

Algorithms

Breadth-First Search

About graph search: "Searching" here means "exploring" a particular graph. Searching will help reveal properties of the graph e.g., is the graph connected? Usually, the input is: vertex set and edges (in no particular order).

Key ideas in breadth-first search: (undirected) Mark all vertices as "unvisited". Initialize a queue (to empty). Find an unvisited vertex and apply breadth-first search to it. Add the vertex's neighbors to the queue. Repeat: extract a vertex from the queue, and add its "unvisited" neighbors to the queue.

Example: Initially, place vertex 0 in the queue.

Dequeue 0 => mark it as visited, and add its unvisited neighbors to queue:

Dequeue 1 => mark it as visited, and add its unvisited neighbors to queue:

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

12/33

3/2/13

Algorithms

Dequeue 2 => mark it as visited, and add its unvisited neighbors to queue:

Dequeue 2 => it's already visited, so ignore. Continuing ...

Breadth-first search tree, and visit order:

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

13/33

3/2/13

Algorithms

Exploring an edge: examining an unvisited neighbor. If an unvisited neighbor gets on the queue for the first time, the edge is called a "tree edge". Putting the tree edges and all vertices together results in: the breadth-first search tree. For a particular graph and its implementation, the tree produced is unique. However, starting from another vertex will result in another tree, that may be just as useful.

In-Class Exercise 7.7: What is the visit-order and breadth-first search tree for this graph:

Searching an unconnected graph: The connected components are explored in order: Example:

The tree, and visit order:

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

14/33

3/2/13

Algorithms

Implementation: There are two varieties: Allow a vertex to have two states: "unvisited" and "visited" An "unvisited" vertex can get placed in the queue multiple times. It's possible to dequeue a "visited" vertex. When dequeueing, check whether vertex was visited. Allow three states: "unvisited", "in-queue", and "visited": When placing a neighbor in the queue for the first time, mark it as "in-queue". Place only "unvisited" neighbors in queue. => Queue has only unique, unvisited vertices. Extra space required. We'll use the former approach, with an adjacency matrix: Pseudocode:
A l g o r i t h m :b r e a d t h F i r s t M a t r i x( a d j M a t r i x ,n ) I n p u t :Ag r a p h ' sa d j a c e n c ym a t r i x ,n u m b e ro fv e r t i c e sn . / /V i s i to r d e rw i l ls t a r tw i t h" 0 " ,s oi n i t i a l i z et o1 . f o ri = 0t on 1 v i s i t O r d e r [ i ]=1 e n d f o r / /Ac o u n t e rf o rt h eo r d e r : v i s i t C o u n t=1 / /S t a n d a r dq u e u ed a t as t r u c t u r e . C r e a t eq u e u e ; / /L o o kf o ra nu n v i s i t e dv e r t e xa n de x p l o r ei t st r e e . / /W en e e dt h i sb e c a u s et h eg r a p hm a yh a v em u l t i p l ec o m p o n e n t s . 6 . 7 . f o ri = 0t on 1 i fv i s i t O r d e r [ i ]<0 / /W ec a l lt h i s" i t e r a t i v e "b e c a u s eo t h e rs e a r c h e sa r er e c u r s i v e . 8 . b r e a d t h F i r s t M a t r i x I t e r a t i v e( i ) 9 . e n d i f 1 0 . e n d f o r

1 . 2 . 3 .

4 .

5 .

A l g o r i t h m :b r e a d t h F i r s t M a t r i x I t e r a t i v e( u ) I n p u t :v e r t e xu ,a d j M a t r i xi sa s s u m e dt ob eg l o b a l . / /Q u e u en e e d st ob er e s e tf o re a c ht r e e . C l e a rq u e u e ;

1 .

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

15/33

3/2/13

Algorithms

2 .

/ /P l a c er o o to ft r e eo nt h eq u e u e . q u e u e . a d d T o R e a r( u ) ; / /C o n t i n u ep r o c e s s i n gv e r t i c e su n t i ln om o r ec a nb ea d d e d . w h i l eq u e u en o te m p t y / /R e m o v eav e r t e x . v=r e m o v ei t e ma tf r o n to fq u e u e ; / /I fi th a s n ' tb e e nv i s i t e d. . . i fv i s i t O r d e r [ v ]<0 / /V i s i tt h ev e r t e x . v i s i t C o u n t=v i s i t C o u n t+1 v i s i t O r d e r [ v ]=v i s i t C o u n t / /L o o kf o rn e i g h b o r st ov i s i t . f o ri = 0t on 1 i fa d j M a t r i x [ v ] [ i ]=1a n di! =v / /C h e c ks e l f l o o p :i! =v q u e u e . a d d T o R e a r( i ) e n d i f e n d f o r e n d i f

3 .

4 . 5 . 6 . 7 . 8 . 9 . 1 0 . 1 1 . 1 2 . 1 3 .

1 4 . e n d w h i l e

In-Class Exercise 7.8: Does the above code identify components? If so, where? If not, how can you modify the code to identify components?

A sample Java implementation: (source file)


i m p o r tj a v a . u t i l . * ; p u b l i cc l a s sU n d i r e c t e d B r e a d t h F i r s t M a t r i x{ i n tn u m V e r t i c e s ; i n tn u m E d g e s ; b o o l e a ni s W e i g h t e d ; b o o l e a nu s e M a t r i x=t r u e ; d o u b l e[ ] [ ]a d j M a t r i x ; / /N u m b e ro fv e r t i c e s ,g i v e na si n p u t . / /W ek e e pt r a c ko ft h en u m b e ro fe d g e s . / /I st h i saw e i g h t e dg r a p h ? / /A d j a c e n c y m a t r i xo rl i s t ? / /T h em a t r i x .N o t e :w eu s e" d o u b l e "t os t o r e / /" d o u b l e "w e i g h t s ,i ft h eg r a p hi sw e i g h t e d . / /v i s i t O r d e r [ i ]=t h ei t hv e r t e xt ob ev i s i t e di no r d e r . / /W ew i l lt r a c kv i s i t sw i t ht h i sc o u n t e r . / /T h eq u e u ef o rb r e a d t h f i r s t ,aj a v a . u t i l . L i n k e d L i s ti n s t a n c e .

i n t [ ]v i s i t O r d e r ; i n tv i s i t C o u n t ; L i n k e d L i s tq u e u e ;

p u b l i cv o i di n i t i a l i z e( i n tn u m V e r t i c e s ,b o o l e a ni s W e i g h t e d ) { / /. . .W e ' v es e e nt h i sb e f o r e. . . }

p u b l i cv o i di n s e r t U n d i r e c t e d E d g e( i n ts t a r t V e r t e x ,i n te n d V e r t e x ,d o u b l ew e i g h t ) { / /. . . }

/ / / /B R E A D T H F I R S TS E A R C H

/ /I n i t i a l i z ev i s i ti n f o r m a t i o nb e f o r es e a r c h . v o i di n i t S e a r c h( ) { / /I M P O R T A N T :a l li n i t i a l i z a t i o n sw i l lu s e" 1 " .W ew i l lt e s t / /e q u a l i t yw i t h" 1 " ,s oi t ' si m p o r t a n tn o tt oc h a n g et h i sc a v a l i e r l y .


www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 16/33

3/2/13

Algorithms

v i s i t C o u n t=1 ; f o r( i n ti = 0 ;i<n u m V e r t i c e s ;i + + ){ v i s i t O r d e r [ i ]=1 ; } }

/ /M a t r i xi m p l e m e n t a t i o no fb r e a d t h f i r s ts e a r c h . v o i db r e a d t h F i r s t M a t r i x( ) { / /1 .I n i t i a l i z ev i s i tv a r i a b l e s . i n i t S e a r c h( ) ; / /2 .C r e a t eaq u e u e . q u e u e=n e wL i n k e d L i s t ( ) ; / /3 .F i n da nu n v i s i t e dv e r t e xa n da p p l yb r e a d t h f i r s ts e a r c ht oi t . / / N o t e :i ft h eg r a p hi sc o n n e c t e d ,t h ec a l lw i t hi = 0w i l lr e s u l t / / i nv i s i t i n ga l lv e r t i c e s .N o n e t h e l e s s ,w ed o n ' tk n o wt h i s / / i na d v a n c e ,s ow en e e dt om a r c ht h r o u g ha l lt h ev e r t i c e s . f o r( i n ti = 0 ;i<n u m V e r t i c e s ;i + + ){ i f( v i s i t O r d e r [ i ]= =1 ){ / /W ec a l li t" i t e r a t i v e "b e c a u s ed e p t h F i r s ts e a r c hi s" r e c u r s i v e " . b r e a d t h F i r s t M a t r i x I t e r a t i v e( i ) ; } } }

/ /A p p l yb r e a d t h f i r s ts e a r c ht oap a r t i c u l a rv e r t e x( r o o to fat r e e ) v o i db r e a d t h F i r s t M a t r i x I t e r a t i v e( i n tu ) { / /1 .Af r e s hq u e u ei su s e df o ran e wb r e a d t h f i r s ts e a r c h . q u e u e . c l e a r ( ) ; / /2 .S t a r tw i t ht h ef i r s tv e r t e x :a d dt oR E A Ro fq u e u eu s i n gj a v a . u t i l . L i n k e d L i s t . a d d L a s t ( ) . / / B e c a u s eL i n k e d L i s ts t o r e so b j e c t s ,w e" o b j e c t i f y "t h ev e r t e x .Am o r ee f f i c i e n t / / i m p l e m e n t a t i o nw o u l du s eas e p a r a t eq u e u eo fi n t ' s . q u e u e . a d d L a s t( n e wI n t e g e r ( u ) ) ; / /3 .A sl o n ga st h eq u e u eh a sv e r t i c e st op r o c e s s . . . w h i l e( !q u e u e . i s E m p t y ( ) ){ / /3 . 1D e q u e u ea n de x t r a c tv e r t e xu s i n gj a v a . u t i l . L i n k e d L i s t . r e m o v e F i r s t ( ) . I n t e g e rV=( I n t e g e r )q u e u e . r e m o v e F i r s t ( ) ; i n tv=V . i n t V a l u e ( ) ; / /3 . 2I ft h ev e r t e xh a sb e e nv i s i t e d ,s k i p . i f( v i s i t O r d e r [ v ]! =1 ) c o n t i n u e ; / /3 . 3O t h e r w i s e ,s e ti t sv i s i to r d e r . v i s i t C o u n t + + ; v i s i t O r d e r [ v ]=v i s i t C o u n t ; / /3 . 4N e x t ,p l a c ei t sn e i g h b o r so nt h eq u e u e . f o r( i n ti = 0 ;i<n u m V e r t i c e s ;i + + ){ / /3 . 4 . 1F i r s t ,c h e c kw h e t h e rv e r t e xii san e i g h b o r . i f(( a d j M a t r i x [ v ] [ i ]>0 )& &( i! =v )){ / /3 . 4 . 1 . 1I fih a s n ' tb e e nv i s i t e d ,p l a c ei nq u e u e . i f( v i s i t O r d e r [ i ]= =1 ){ q u e u e . a d d L a s t( n e wI n t e g e r ( i ) ) ; } } }/ /e n d f o r }/ /e n d w h i l e
www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 17/33

3/2/13

Algorithms

} }/ /e n d c l a s s

Analysis: adjacency matrix Assume V vertices and E edges. Each vertex is processed V times (worst-case) in starting a tree. => O(V). Searching for a neighbor: O(V) (scan through matrix). => all scans take O(V2 ). Each queue operation is O(1). Each edge is processed once: => O(E) queue operations and O(E) vertex manipulations. Total: O(V2 + E) = O(V2 ). Analysis: adjacency list Searching for a neighbor: O(# neighbors) => total neighbor searches is O(E) (Why?) Other operations are the same Total: O(V + E) = O(E). In-Class Exercise 7.9: Which is better: adjacency matrix or adjacency list?

About O(V + E): Note that O(V + E) = O(E) => it is written as O(V + E) just for emphasis. O(V + E) is optimal: Every vertex and every edge must be examined. => O(V + E) => not possible to do better than O(V + E) BFS (with adjacency list) is an example of an optimal algorithm.

Directed graphs: BFS in directed graphs is similar, except that a vertex's "neighbor" is one that's reachable by an edge going out from the vertex. => search code is identical (but insertion code is different).

Applications: Connectivity: Breadth-first search identifies connected components. However, depth-first search is preferred (required for directed graphs).
www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 18/33

3/2/13

Algorithms

Shortest paths: A path between two vertices in the tree is the shortest path in the graph. Optimization algorithms: Various problems result in "graph search space". BFS together with "exploration rules" is often used to search for solutions (e.g., branch-and-bound exploration). Note: BFS works on a weighted graph by ignoring the weights and only using connectivity information (i.e., is there an edge or not?).

Depth-First Search on Undirected Graphs

Key ideas: Mark all vertices as "unvisited". Visit first vertex. Recursively visit its "unvisited" neighbors.

Example: Start with vertex 0 and mark it visited.

Visit the first neighbor 1, mark it visited.

Explore 1's first unvisited neighbor, 2.

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

19/33

3/2/13

Algorithms

Continuing until all vertices are visited ...

Vertices are marked in order of visit. An edge to an unvisited neighbor that gets visited next is in the depth-first search tree.

In-Class Exercise 7.10: What is the visit-order and depth-first search tree for this graph:

Then, apply breadth-first search, but using a stack instead of a queue. Show the stack contents.

Implementation: Easier than breadth-first search. Pseudocode: adjacency matrix


A l g o r i t h m :d e p t h F i r s t M a t r i x( a d j M a t r i x ,n ) I n p u t :Ag r a p h ' sa d j a c e n c ym a t r i x ,n u m b e ro fv e r t i c e sn . / /V i s i to r d e rw i l ls t a r tw i t h" 0 " ,s oi n i t i a l i z et o1 . f o ri = 0t on 1 v i s i t O r d e r [ i ]=1 e n d f o r / /Ac o u n t e rf o rt h eo r d e r : v i s i t C o u n t=1
20/33

1 . 2 . 3 .

4 .

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

3/2/13

Algorithms

5 . 6 . 7 . 8 . 9 .

/ /L o o kf o ra nu n v i s i t e dv e r t e xa n de x p l o r ei t st r e e . / /W en e e dt h i sb e c a u s et h eg r a p hm a yh a v em u l t i p l ec o m p o n e n t s . f o ri = 0t on 1 i fv i s i t O r d e r [ i ]<0 d e p t h F i r s t M a t r i x R e c u r s i v e( i ) e n d i f e n d f o r

A l g o r i t h m :d e p t h F i r s t M a t r i x R e c u r s i v e( v ) I n p u t :v e r t e xv ,a d j M a t r i xi sa s s u m e dt ob eg l o b a l . / /M a r kv e r t e xva sv i s i t e d . v i s i t C o u n t=v i s i t C o u n t+1 v i s i t O r d e r [ v ]=v i s i t C o u n t / /L o o kf o rf i r s tu n v i s i t e dn e i g h b o r . f o ri = 0t on 1 i fa d j M a t r i x [ v ] [ i ]>0a n di! =v i fv i s i t O r d e r [ i ]<0 / /I fu n v i s i t e dv i s i tr e c u r s i v e l y . d e p t h F i r s t M a t r i x R e c u r s i v e( i ) e n d i f e n d i f e n d f o r

1 . 2 .

3 . 4 . 5 . 6 . 7 . 8 . 9 .

Completion order: In breadth-first search, once a vertex is processed, it is never processed again. In depth-first, we also encounter a vertex after returning from the recursive call. => we can record a completion order.

To record the completion order, simply increment the completion-counter after the recursion:
A l g o r i t h m :d e p t h F i r s t M a t r i x R e c u r s i v e( v ) I n p u t :v e r t e xv ,a d j M a t r i xi sa s s u m e dt ob eg l o b a l . / /M a r kv e r t e xva sv i s i t e d . v i s i t C o u n t=v i s i t C o u n t+1 v i s i t O r d e r [ v ]=v i s i t C o u n t / /L o o kf o rf i r s tu n v i s i t e dn e i g h b o r . f o ri = 0t on 1 i fa d j M a t r i x [ v ] [ i ]>0a n di! =v / /C h e c ks e l f l o o p . i fv i s i t O r d e r [ i ]<0 / /I fu n v i s i t e dv i s i tr e c u r s i v e l y . d e p t h F i r s t M a t r i x R e c u r s i v e( i ) e n d i f e n d i f e n d f o r / /A f t e rr e t u r n i n gf r o mr e c u r s i o n ,s e tc o m p l e t i o no r d e r :
www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 21/33

1 . 2 .

3 . 4 . 5 . 6 . 7 . 8 . 9 .

3/2/13

Algorithms

1 0 . c o m p l e t i o n C o u n t=c o m p l e t i o n C o u n t+1 1 1 . c o m p l e t i o n O r d e r [ v ]=c o m p l e t i o n C o u n t

Note: c o m p l e t i o n C o u n tand c o m p l e t i o n O r d e rneed to be initialized (not shown) in d e p t h F i r s t M a t r i x . Sample Java code: (source file)
p u b l i cc l a s sU n d i r e c t e d D e p t h F i r s t M a t r i x{ i n tn u m V e r t i c e s ; i n tn u m E d g e s ; b o o l e a ni s W e i g h t e d ; b o o l e a nu s e M a t r i x=t r u e ; d o u b l e[ ] [ ]a d j M a t r i x ; / /N u m b e ro fv e r t i c e s ,g i v e na si n p u t . / /W ek e e pt r a c ko ft h en u m b e ro fe d g e s . / /I st h i saw e i g h t e dg r a p h ? / /A d j a c e n c y m a t r i xo rl i s t ? / /T h em a t r i x .N o t e :w eu s e" d o u b l e "t os t o r e / /" d o u b l e "w e i g h t s ,i ft h eg r a p hi sw e i g h t e d . / /v i s i t O r d e r [ i ]=t h ei t hv e r t e xt ob ev i s i t e di no r d e r . / /W ew i l lt r a c kv i s i t sw i t ht h i sc o u n t e r . / /c o m p l e t i o n O r d e r [ i ]=t h ei t hv e r t e xt h a tc o m p l e t e d . / /F o rt r a c k i n g .

i n t [ ]v i s i t O r d e r ; i n tv i s i t C o u n t ; i n t [ ]c o m p l e t i o n O r d e r ; i n tc o m p l e t i o n C o u n t ;

p u b l i cv o i di n i t i a l i z e( i n tn u m V e r t i c e s ,b o o l e a ni s W e i g h t e d ) { / /. . . }

p u b l i cv o i di n s e r t U n d i r e c t e d E d g e( i n ts t a r t V e r t e x ,i n te n d V e r t e x ,d o u b l ew e i g h t ) { / /. . . }

/ / / /D E P T H F I R S TS E A R C H / /I n i t i a l i z ev i s i ti n f o r m a t i o nb e f o r es e a r c h . v o i di n i t S e a r c h( ) { / /I M P O R T A N T :a l li n i t i a l i z a t i o n sw i l lu s e" 1 " .W ew i l lt e s t / /e q u a l i t yw i t h" 1 " ,s oi t ' si m p o r t a n tn o tt oc h a n g et h i sc a v a l i e r l y . v i s i t C o u n t=1 ; c o m p l e t i o n C o u n t=1 ; f o r( i n ti = 0 ;i<n u m V e r t i c e s ;i + + ){ v i s i t O r d e r [ i ]=1 ; c o m p l e t i o n O r d e r [ i ]=1 ; } }

/ /M a t r i xi m p l e m e n t a t i o no fd e p t h f i r s ts e a r c h . v o i dd e p t h F i r s t M a t r i x( ) { / /1 .I n i t i a l i z ev i s i tv a r i a b l e s( s a m ei n i t i a l i z a t i o nf o rb r e a d t h f i r s ts e a r c h ) . i n i t S e a r c h( ) ; / /2 .F i n da nu n v i s i t e dv e r t e xa n da p p l yd e p t h f i r s ts e a r c ht oi t . / / N o t e :i ft h eg r a p hi sc o n n e c t e d ,t h ec a l lw i t hi = 0w i l lr e s u l t / / i nv i s i t i n ga l lv e r t i c e s .N o n e t h e l e s s ,w ed o n ' tk n o wt h i s / / i na d v a n c e ,s ow en e e dt om a r c ht h r o u g ha l lt h ev e r t i c e s . f o r( i n ti = 0 ;i<n u m V e r t i c e s ;i + + ){ i f( v i s i t O r d e r [ i ]<0 ){ d e p t h F i r s t M a t r i x R e c u r s i v e( i ) ; } } }

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

22/33

3/2/13

Algorithms

/ /R e c u r s i v ev i s i t i n go fv e r t i c e ss t a r t i n gf r o mav e r t e x . v o i dd e p t h F i r s t M a t r i x R e c u r s i v e( i n tv ) { / /1 .F i r s t ,v i s i tt h eg i v e nv e r t e x .N o t e :v i s i t C o u n ti sag l o b a l . v i s i t C o u n t + + ; v i s i t O r d e r [ v ]=v i s i t C o u n t ; / /2 .N o wf i n du n v i s i t e dc h i l d r e na n dv i s i tt h e mr e c u r s i v e l y . f o r( i n ti = 0 ;i<n u m V e r t i c e s ;i + + ){ / /2 . 1 C h e c kw h e t h e rv e r t e xii san e i g h b o ra n da v o i ds e l f l o o p s . i f(( a d j M a t r i x [ v ] [ i ]>0 )& &( i! =v )){ i f( v i s i t O r d e r [ i ]= =1 ){ / /ii sa nu n v i s i t e dn e i g h b o r . d e p t h F i r s t M a t r i x R e c u r s i v e( i ) ; } } } / /3 .A f t e rr e t u r n i n gf r o mr e c u r s i o n ( s ) ,s e tt h ep o s t o r d e ro r" c o m p l e t i o n "o r d e rn u m b e r . c o m p l e t i o n C o u n t + + ; c o m p l e t i o n O r d e r [ v ]=c o m p l e t i o n C o u n t ; } }/ /e n d c l a s s

"Back" and "Down" edges: Consider the previous example:

Back edge When vertex 2 probes neighbor 0, 0 has already been visited. Vertex 0 is higher up in the tree (ancestor of 2) => 0 has a lower visitOrder => visitOrder[0] < visitOrder[2]. The exploration from 2 to 0 is called a back edge. Down edge: When vertex 3 probes neighbor 6, 6 has already been visited. Vertex 6 is lower down in the tree (descendant of 3) visitOrder[6] > visitOrder[3] The exploration from 3 to 6 is called a down edge.
www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 23/33

3/2/13

Algorithms

Let's modify the code to detect back and down edges:


A l g o r i t h m :d e p t h F i r s t M a t r i x R e c u r s i v e( u ,v ) I n p u t :t h ev e r t e xuf r o mw h i c ht h i si sb e i n gc a l l e d ,v e r t e xv t ob ee x p l o r e d ,a d j M a t r i xi sa s s u m e dt ob eg l o b a l . / /M a r kv e r t e xva sv i s i t e d . v i s i t C o u n t=v i s i t C o u n t+1 v i s i t O r d e r [ v ]=v i s i t C o u n t / /L o o kf o rf i r s tu n v i s i t e dn e i g h b o r . f o ri = 0t on 1 i fa d j M a t r i x [ v ] [ i ]>0a n di! =v / /C h e c ks e l f l o o p . i fv i s i t O r d e r [ i ]<0 / /I fu n v i s i t e dv i s i tr e c u r s i v e l y . d e p t h F i r s t M a t r i x R e c u r s i v e( i ) e l s ei f( i! =u ) / /A v o i dt r i v i a lc a s e . i fv i s i t O r d e r [ i ]<v i s i t O r d e r [ v ] / /F o u n dab a c ke d g e . n u m B a c k E d g e s=n u m B a c k E d g e s+1 e l s e / /F o u n dad o w ne d g e . n u m D o w n E d g e s=n u m D o w n E d g e s+1 e n d i f e n d i f / /v i s i t O r d e r<0 e n d i f / /a d j M a t r i x [ v ] [ i ]>0 e n d f o r

1 . 2 .

3 . 4 . 5 . 6 . 7 . 8 . 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 .

/ /A f t e rr e t u r n i n gf r o mr e c u r s i o n ,s e tc o m p l e t i o no r d e r : 1 6 . c o m p l e t i o n C o u n t=c o m p l e t i o n C o u n t+1 1 7 . c o m p l e t i o n O r d e r [ v ]=c o m p l e t i o n C o u n t

Why identify back and down edges? A graph with no back edges revealed by DFS is an acyclic graph. Down edges are useful in identifying so-called articulation edges.

Identifying connected components: Depth-first search is most often used for identifying connected components in an undirected graph. Key ideas: Every time depth-first is re-started (in depthFirstMatrix) a new component has been found. create a new component label. In the recursion (depthFirstMatrixRecursive), simply identify each vertex with the current component label. Let's re-write (part of) the pseudocode to identify components:
A l g o r i t h m :d e p t h F i r s t M a t r i x( a d j M a t r i x ,n ) I n p u t :Ag r a p h ' sa d j a c e n c ym a t r i x ,n u m b e ro fv e r t i c e sn . / /V i s i to r d e rw i l ls t a r tw i t h" 0 " ,s oi n i t i a l i z et o1 . f o ri = 0t on 1 v i s i t O r d e r [ i ]=1 e n d f o r / /Ac o u n t e rf o rt h eo r d e r : v i s i t C o u n t=1 c o m p l e t i o n C o u n t=1 c u r r e n t C o m p o n e n t L a b e l=1 / /L o o kf o ra nu n v i s i t e dv e r t e xa n de x p l o r ei t st r e e .
www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 24/33

1 . 2 . 3 .

4 . 5 . 6 .

3/2/13

Algorithms

/ /W en e e dt h i sb e c a u s et h eg r a p hm a yh a v em u l t i p l ec o m p o n e n t s . 7 . f o ri = 0t on 1 8 . i fv i s i t O r d e r [ i ]<0 9 . c u r r e n t C o m p o n e n t L a b e l=c u r r e n t C o m p o n e n t L a b e l+1 1 0 . d e p t h F i r s t M a t r i x R e c u r s i v e( i ) 1 1 . e n d i f 1 2 . e n d f o r

A l g o r i t h m :d e p t h F i r s t M a t r i x R e c u r s i v e( v ) I n p u t :v e r t e xv ,a d j M a t r i xi sa s s u m e dt ob eg l o b a l . / /M a r kv e r t e xva sv i s i t e d . v i s i t C o u n t=v i s i t C o u n t+1 v i s i t O r d e r [ v ]=v i s i t C o u n t / /M a r kt h ec o m p o n e n tl a b e l c o m p o n e n t L a b e l [ v ]=c u r r e n t C o m p o n e n t L a b e l / /L o o kf o rf i r s tu n v i s i t e dn e i g h b o r . . . / /. . .e t c( s a m ea se a r l i e rv e r s i o n )

1 . 2 .

3 .

4 .

Sample Java code: (source file).

Analysis: adjacency matrix Same as breadth-first search: O(V2 ) Why? O(1) work for processing each vertex (except for identifying neighbors). O(V) work for identifying neighbors. => O(V2 ) overall. Analysis: adjacency list Similar analysis (to breadth-first search) gives: O(V + E). DFS with adjacency list is optimal.

Applications: Connectivity: identifying connected components. => which earlier-stated problem would this solve? Cycle existence. Others: finding articulation edges, vertices, "bipartiteness". Identifying equivalence classes

Depth-First Search in Directed Graphs

Key ideas: A straightforward depth-first search is similar to the undirected version => only explore edges going outward from a vertex in a directed graph. In addition to "back" and "down" edges, it is useful to identify "cross" edges.
www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 25/33

3/2/13

Algorithms

Example: Consider: (slightly different from previous example)

Applying DFS gives:

Let's modify the pseudocode to detect cross edges as well: Key ideas: What used to be a back edge in the undirected version is now either a back edge or cross edge. Back or down edge: when the vertex on the other side is "visited" but not yet "completed". Cross edge: when the vertex on the other side is "completed". Pseudocode:
A l g o r i t h m :d e p t h F i r s t M a t r i x R e c u r s i v e( u ,v ) I n p u t :t h ev e r t e xuf r o mw h i c ht h i si sb e i n gc a l l e d ,v e r t e xv t ob ee x p l o r e d ,a d j M a t r i xi sa s s u m e dt ob eg l o b a l . / /M a r kv e r t e xva sv i s i t e d . v i s i t C o u n t=v i s i t C o u n t+1 v i s i t O r d e r [ v ]=v i s i t C o u n t / /L o o kf o rf i r s tu n v i s i t e dn e i g h b o r . f o ri = 0t on 1 i fa d j M a t r i x [ v ] [ i ]>0a n di! =v / /C h e c ks e l f l o o p . i fv i s i t O r d e r [ i ]<0 / /I fu n v i s i t e dv i s i tr e c u r s i v e l y . d e p t h F i r s t M a t r i x R e c u r s i v e( i ) e l s ei f( i! =u ) / /A v o i dt r i v i a lc a s e . i fc o m p l e t i o n O r d e r [ i ]<0 / /N o tf i n i s h e dp r o c e s s i n giy e t
26/33

1 . 2 .

3 . 4 . 5 . 6 . 7 . 8 .

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

3/2/13

Algorithms

9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 .

/ /F o u n dab a c ke d g e . n u m B a c k E d g e s=n u m B a c k E d g e s+1 e l s ei fv i s i t O r d e r [ i ]>v i s i t O r d e r [ v ] / /F o u n dad o w ne d g e . n u m D o w n E d g e s=n u m D o w n E d g e s+1 e l s e / /F o u n dac r o s se d g e n u m C r o s s E d g e s=n u m C r o s s E d g e s+1 e n d i f e n d i f / /v i s i t O r d e r<0 e n d i f / /a d j M a t r i x [ v ] [ i ]>0 e n d f o r

/ /A f t e rr e t u r n i n gf r o mr e c u r s i o n ,s e tc o m p l e t i o no r d e r : 1 8 . c o m p l e t i o n C o u n t=c o m p l e t i o n C o u n t+1 1 9 . c o m p l e t i o n O r d e r [ v ]=c o m p l e t i o n C o u n t

Analysis: (same as undirected case) Adjacency matrix: O(V2 ). Adjacency list: O(V+E).

Strongly-Connected Components in Directed Graphs

Finding components: Recall, in a directed graph a strongly-connected component is a set of vertices, along with edges associated with those vertices, such that there is a path from every vertex in that set to every other in that set. Example: vertices 0,1,2 form a strongly-connected component.

In-Class Exercise 7.11: Describe in pseudocode a simple algorithm to find strongly-connected components that uses the above DFS algorithm for directed graphs. Hint: vertices i and j are in the same strongly-connected component if i is reachable from j and j is reachable from i. How long does your algorithm take? We will look at two different algorithms for finding strongly-connected components: Kosaraju's Algorithm: this applies DFS twice but in an unusual way. Tarjan's Algorithm: computes the components directly but is harder to understand.

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

27/33

3/2/13

Algorithms

Kosaraju's algorithm: First, we need to understand the reverse of a directed graph. Suppose G = (V,E) is a graph. G' = (V,E') is the reverse of G if E'=E with the direction of edges reversed.

Next, recall completion-order later completion order => closer to root The algorithm has two phases: First phase: find completion order on reverse graph using standard DFS. Second phase: use a modified DFS on original graph in which completion order is used as a priority. Let's describe the pseudocode first:
A l g o r i t h m :s t r o n g l y C o n n e c t e d C o m p o n e n t s( a d j M a t r i x ,n ) I n p u t :Ag r a p h ' sa d j a c e n c ym a t r i x ,n u m b e ro fv e r t i c e sn . 1 . 2 . 3 . 4 . 5 . G '=r e v e r s e( G ) c o m p l e t i o n O r d e r=d e p t h F i r s t S e a r c h( G ' ) s o r t O r d e r=s o r tv e r t i c e si nr e v e r s eo r d e ro fc o m p l e t i o n c o m p o n e n t L a b e l s=m o d i f i e d D e p t h F i r s t S e a r c h( G ,s o r t O r d e r ) r e t u r nc o m p o n e n t L a b e l s

/ /L a s tt of i r s t .

A l g o r i t h m :m o d i f i e d D e p t h F i r s t S e a r c h( a d j M a t r i x ,s o r t O r d e r ) I n p u t :Ag r a p h ' sa d j a c e n c ym a t r i x ,as o r t( p r i o r i t y )o r d e ro fv e r t i c e s 1 . 2 . 3 . 4 . 5 . f o ri = 0t on 1 v i s i t O r d e r [ i ]=1 e n d f o r v i s i t C o u n t=1 c u r r e n t C o m p o n e n t L a b e l=1

/ /L o o kf o ra nu n v i s i t e dv e r t e xa n de x p l o r ei t st r e e . 6 . f o ri = 0t on 1 7 . v=s o r t O r d e r [ i ] / /I no r d e ro fc o m p l e t i o no fr e v e r s es e a r c h . 8 . i fv i s i t O r d e r [ v ]<0 9 . c u r r e n t C o m p o n e n t L a b e l=c u r r e n t C o m p o n e n t L a b e l+1 1 0 . m o d i f i e d D e p t h F i r s t M a t r i x R e c u r s i v e( v ) 1 1 . e n d i f 1 2 . e n d f o r 1 3 . r e t u r n c o m p o n e n t L a b e l s

A l g o r i t h m :m o d i f i e d D e p t h F i r s t M a t r i x R e c u r s i v e( v ) / /M a r kv e r t e xva sv i s i t e da n dr e c o r dc o m p o n e n tl a b e l . v i s i t C o u n t=v i s i t C o u n t+1 v i s i t O r d e r [ v ]=v i s i t C o u n t c o m p o n e n t L a b e l [ v ]=c u r r e n t C o m p o n e n t L a b e l / /L o o kf o rf i r s tu n v i s i t e dn e i g h b o r . f o ri = 0t on 1 u=s o r t O r d e r [ i ] / /I no r d e ro fc o m p l e t i o no fr e v e r s es e a r c h .


28/33

1 . 2 . 3 .

4 .

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

3/2/13

Algorithms

5 . i fa d j M a t r i x [ v ] [ u ]>0a n du! =v 6 . i fv i s i t O r d e r [ u ]<0 7 . d e p t h F i r s t M a t r i x R e c u r s i v e( u ) 8 . e n d i f 9 . e n d i f 1 0 . e n d f o r

Why does this work? The proof is in two parts. Consider two vertices u and v . Part (1): If u and v are mutually reachable, does the algorithm report that they are in the same component? Suppose u is visited before v in the second-phase.

Then, DFS will certainly visit v . => The algorithm reports both in the same component. The same holds if v is visited before u. Part (2): If the algorithm reports that u and v are in the same component, is that in fact always true? Because u and v are in the same reported component, they are part of a single tree => Let w be the root of this tree. Then w was visited before u and v . => w has a later phase-1 completion time than u and v . (Store this fact for now).

Next, observe that we reached u from w in the second phase => there's a directed path from w to u in G. => there's a directed path from u to w in G'. Suppose there is no path from w to u in G' (the reverse).

=> Then, the path from u to w in G' would result in u having a later completion time
www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html 29/33

3/2/13

Algorithms

=> Contradiction. Thus, there is a path from w to u in G'. => There is a path in G from u to w, Which means, there are paths in G from w to u and back. The same argument shows that there is path from w to v and back. => u and v are mutually reachable in G.

Tarjan's Algorithm: Some intuition: Consider what happens when we complete processing "3" in

How can we identify the strong component "3, 4, 6"? First, there is no back edge that goes from "4, 6" to any node "above" 3. Second, both "4" and "6" were visited in the descent from "3". But nodes "above" 3 will have a lower visitOrder. => keep track of lowest visitOrder reachable by a back edge from a "potential component". How to mark vertices in a "component"? Build a stack. Place vertices on the stack as you recurse down. When coming back up the recursion, if you've identified a component, all vertices in the component are going to be at the top of the stack. Example above: "3, 4, 6" will be on the stack when returning to "3". Another example: "0, 1, 2" will be at the top when returning to "0". (It won't be removed along the way back from "3" to "0"). Pseudocode:
A l g o r i t h m :d e p t h F i r s t M a t r i x R e c u r s i v e( u ,v ) I n p u t :t h ev e r t e xuf r o mw h i c ht h i si sb e i n gc a l l e d ,v e r t e xv t ob ee x p l o r e d ,a d j M a t r i xi sa s s u m e dt ob eg l o b a l . / /M a r kv e r t e xva sv i s i t e d . v i s i t C o u n t=v i s i t C o u n t+1 v i s i t O r d e r [ v ]=v i s i t C o u n t / /C u r r e n tl o w e s tr e a c h a b l e : l o w e s t R e a c h a b l e [ v ]=v i s i t O r d e r [ v ] ; / /I m p o r t a n t :m i n L o w e s t R e a c h a b l ei sal o c a lv a r i a b l es ot h a ta / /f r e s hv e r s i o ni su s e di ne a c hr e c u r s i v ec a l l . m i n L o w e s t R e a c h a b l e=l o w e s t R e a c h a b l e [ v ] ; / /I n i t i a l i z es t a c k T o pt o0o u t s i d e .s t a c k T o pp o i n t st on e x ta v a i l a b l e . s t a c k [ s t a c k T o p ]=v ; s t a c k T o p+ + ;
30/33

1 . 2 .

3 .

4 .

5 . 6 .

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

3/2/13

Algorithms

7 . 8 . 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 .

/ /L o o kf o rf i r s tu n v i s i t e dn e i g h b o r . f o ri = 0t on 1 i fa d j M a t r i x [ v ] [ i ]>0a n di! =v / /C h e c ks e l f l o o p . i fv i s i t O r d e r [ i ]<0 / /I fu n v i s i t e dv i s i tr e c u r s i v e l y . d e p t h F i r s t M a t r i x R e c u r s i v e( i ) e l s ei f( i! =u ) / /A v o i dt r i v i a lc a s e . / /I d e n t i f yb a c k ,d o w na n dc r o s se d g e s. . .( n o ts h o w n ) e n d i f / /v i s i t O r d e r<0 i fl o w e s t R e a c h a b l e [ i ]<m i n L o w e s t R e a c h a b l e m i n L o w e s t R e a c h a b l e=l o w e s t R e a c h a b l e [ i ] e n d i f e n d i f / /a d j M a t r i x [ v ] [ i ]>0 e n d f o r / /W er e a c hh e r ea f t e ra l ln e i g h b o r sh a v eb e e ne x p l o r e d . / /C h e c kw h e t h e ras t r o n gc o m p o n e n th a sb e e nf o u n d . i fm i n L o w e s t R e a c h a b l e<l o w e s t R e a c h a b l e [ v ] / /T h i si sn o tac o m p o n e n t ,b u tw en e e dt ou p d a t et h e / /l o w e s t R e a c h a b l ef o rt h i sv e r t e xs i n c ei t sa n c e s t o r sw i l l / /u s et h i sv a l u eu p o nr e t u r n i n g . l o w e s t R e a c h a b l e [ v ]=m i n L o w e s t R e a c h a b l e r e t u r n e n d i f

1 8 .

1 9 . 2 0 . 2 1 .

/ /W e ' v ef o u n dac o m p o n e n t . 2 2 . d o / /G e tt h es t a c kt o p . 2 3 . s t a c k T o p=s t a c k T o p-1 2 4 . u=s t a c k [ s t a c k T o p ] ; / /W es t a r tc u r r e n t S t r o n g C o m p o n e n t L a b e lf r o m" 0 " . 2 5 . s t r o n g C o m p o n e n t L a b e l s [ u ]=c u r r e n t S t r o n g C o m p o n e n t L a b e l ; / /S e tt h i st oah i g hv a l u es ot h a ti td o e sn o ta f f e c t / /f u r t h e rc o m p a r i s o n su pt h eD F St r e e . 2 6 . l o w e s t R e a c h a b l e [ u ]=n u m V e r t i c e s ; 2 7 . w h i l es t a c k [ s t a c k T o p ]! =v 2 8 . c u r r e n t S t r o n g C o m p o n e n t L a b e l=c u r r e n t S t r o n g C o m p o n e n t L a b e l+1 / /A f t e rr e t u r n i n gf r o mr e c u r s i o n ,s e tc o m p l e t i o no r d e r . . .( n o ts h o w n )

Directed Acyclic Graphs (DAG's)

What are they? A DAG is a directed graph without a cycle => no path can cause you to revisit a vertex. Example:

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

31/33

3/2/13

Algorithms

Example application: task scheduling A large activity consists of many related tasks. Some tasks need to occur before others. => typically a precedence relation among tasks. Represent task precedence using a DAG.

Sequential scheduling: Suppose the tasks represent programs that must be executed on a sequential processor. Goal: find an execution sequence that does not violate precedence constraints. Example: 3 0 1 2 5 4 6 7 Note: 4 0 1 2 5 3 6 7 violates precedence requirements.

Topological sort: A topological sort of a DAG is a sequence of vertices that: does not violate precedence; contains all the vertices. A simple (the "classic") algorithm: Find a vertex that has no predecessors (zero in-degree) (there must be one, or it's not a DAG). Add this to the sequence. Remove it from the graph. In removing, adjust the in-degree of every neighbor of the removed vertex. Repeat. In-Class Exercise 7.12: Apply the classic algorithm to the above example.

Using depth-first search: Key observation: the vertex whose completion occurs first in depth-first search can be placed last in the sequence.

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

32/33

3/2/13

Algorithms

Why? It has no successors (descendants in the DFS tree). Steps: Examine completionOrder in depth-first search. Place first vertex to complete at end of sequence. Remove it from the graph. Place next vertex to complete in next-to-last in sequence. ... and so on ... Note: after DFS "completes" a vertex, the vertex is never seen again => no need to remove it from graph! Example:

Pseudocode: We only need to record topological sort order every time a completion occurs. Partial pseudocode:
A l g o r i t h m :d e p t h F i r s t M a t r i x R e c u r s i v e( u ,v ) I n p u t :t h ev e r t e xuf r o mw h i c ht h i si sb e i n gc a l l e d ,v e r t e xv t ob ee x p l o r e d ,a d j M a t r i xi sa s s u m e dt ob eg l o b a l . / /M a r kv e r t e xva sv i s i t e d . v i s i t C o u n t=v i s i t C o u n t+1 v i s i t O r d e r [ v ]=v i s i t C o u n t / /L o o kf o rf i r s tu n v i s i t e dn e i g h b o ra n dr e c u r s e f o ri = 0t on 1 / /. . .( s a m ea sb e f o r e ,n o ts h o w n ). . . e n d f o r

1 . 2 .

3 . 4 .

/ /A f t e rr e t u r n i n gf r o mr e c u r s i o n ,s e tc o m p l e t i o no r d e r : 5. c o m p l e t i o n C o u n t=c o m p l e t i o n C o u n t+1 6 . c o m p l e t i o n O r d e r [ v ]=c o m p l e t i o n C o u n t / /S e tt o p o l o g i c a ls o r to r d e r . / /I n i t i a l i z et o p S o r t C o u n tt o1o u t s i d et h i sm e t h o d t o p S o r t C o u n t=t o p S o r t C o u n t+1 t o p S o r t O r d e r [ t o p S o r t C o u n t ]=v

7 . 8 .

www.seas.gwu.edu/~simhaweb/alg/lectures/module7/module7.html

33/33