Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph...

46
Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity Joost-Pieter Katoen Formal Methods and Tools Group E-mail: [email protected] October 1, 2002 c JPK

Transcript of Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph...

Page 1: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

Elementary Graph AlgorithmsLecture #6 of Algorithms, Data structures and Complexity

Joost-Pieter Katoen

Formal Methods and Tools Group

E-mail: [email protected]

October 1, 2002

c� JPK

Page 2: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Overview� Graphs

– Graphs, subgraphs, connected graphs, paths, cycles– Representing graphs

� Traversing graphs

– Depth-first search– Breadth-first search

� Directed acyclic graphs

– Topological sorting– Critical path analysis

� Strongly connected components

c� JPK 1

Page 3: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

The importance of graphs

Graphs are used in many (computer science) applications:

� Computer networks

� Representation of topological informations (maps, � � � )

� Representation of electronic circuits

� Precedence graphs, workflows, � � �

� Semantic networks (e.g., entity-relationship diagrams)

we concentrate on fundamental graph algorithms

c� JPK 2

Page 4: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

What is a directed graph?

� A directed graph (or: digraph) � is a pair ��� ��� � with

– � is a set of vertices– � � � is a set of ordered pairs of vertices, called (directed) edges– if is a set of unordered pairs, � is called undirected

� Example:

– � ��� ��� � � ��� �

– �� � ��� � �� � ��� � �� � � � �� � � � �� � �� � �� � ��� � �� � � � �� � �� � �

edge vertex

� � ���

c� JPK 3

Page 5: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Graphs – Terminology

� A subgraph of a graph � � � � � � � is a graph ��� � � � � � � � � with

– ��� � and �

– as �� is a graph it follows � ��� � ���

– if ��� � � and � � , �� is a proper subgraph

� Graph � is symmetric if ��� � ��� � implies � � � ��� �

– every undirected graph has a corresponding symmetric digraph

� Graph � is complete if each pair of vertices is connected via an edge

� The transpose of � is �� � ��� � � � � with ��� � ��� � � iff � � � ��� �

– the direction of each edge in � is reversed in the transposed graph

c� JPK 4

Page 6: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Terminology

subgraphcomplete (and symmetric) digraph

on four nodes

� ��

��

� ��

c� JPK 5

Page 7: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Paths and cycles

� A path from vertex� to is a sequence of edges

� � � � � � � � � � ��� � � � such that � � � and � � �

– the length of a path is the number of vertices it visits minus one

� Vertex is reachable from� if there is a path from� to

� A cycle is a non-empty path with the same first and the last vertex

– a cycle of the form � � is called a self-cycle (or self-loop)– a graph is acyclic if it has no cycles– a cycle of an undirected graph may only visit edges in the same orientation

c� JPK 6

Page 8: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Paths and cycles

self−cycle

cycle

� � ���

� � � � and � � � are example paths

c� JPK 7

Page 9: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Connected graphs

� Undirected graph � :

– � is connected if each vertex is reachable from any other vertex– a connected component of � is a maximal connected subgraph of �

– a graph in a collection of graphs is maximal if it is no proper subgraph of any ofthese graphs

� Directed graph � :

– � is strongly connected if each vertex is reachable from any other vertex– � is weakly connected if, after making all edges undirected, the resulting

undirected graph is connected– a strongly connected component of � is a maximal strongly connected

subgraph of �

� A non-connected graph can be uniquely partitioned into separateconnected components

c� JPK 8

Page 10: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Connected undirected graphs

A B C

D E

F

G H

I

J

c� JPK 9

Page 11: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Connected undirected graphs

A B C

D E

F

G H

I

J

c� JPK 10

Page 12: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Strongly connected components

connected component

a non-connected digraph partitioned into its maximal connected subgraphs

c� JPK 11

Page 13: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Graph representations

Let � � ��� � � � with �� � � � and �� � � � and� � � � � � � � � � � � �

� Adjacency matrix representation

– an � � � matrix� with� �� � � � if� � � � � ��� , and 0 otherwise– if � is undirected,� is symmetric (� � � ; only half of it needs to be stored)

� storage requirement is � � � � �

� Array of adjacency lists representation

– an array indexed by vertex number containing linked lists (adjacency lists)– the� -th array entry contains all edges of � that “leave” � �– if � is undirected an edge is stored twice– edges that are not in � do not require any storage

� storage complexity for sparse � is � �� �

c� JPK 12

Page 14: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Representing an undirected graph

adjacency list representation

� �

� �

��

��

��

��

��

��

��

��

��

� � � � �

� � � � �

� � � � �

� � � � �

� � � � �

adjacency-matrix representation

c� JPK 13

Page 15: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Representating a directed graph

adjacency list representation

��

��

��

��

��

��

� � ���

��

��

��

��

� � � � � �

� � � � � �

� � � � � �

� � � � � �

� � � � � �

� � � � � �

adjacency-matrix representation

c� JPK 14

Page 16: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Overview� Graphs

– Graphs, subgraphs, connected graphs, paths, cycles– Representing graphs

� Traversing graphs

– Depth-first search– Breadth-first search

� Directed acyclic graphs

– Topological sorting– Critical path analysis

� Strongly connected components

c� JPK 15

Page 17: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Traversing graphs

Many algorithms on graphs examine each vertex (and edge)

� Traversal strategies that visit each vertex (or edge) exactly once:

– depth-first search– breadth-first search

� Generalizations of tree-based traversal strategies

– except that we need to keep track of already discovered vertices

� Algorithms based on these searching strategies are in � � �� ��� �� � �

– finding (strongly) connected components– topological sorting– critical path anaylysis– finding biconnected components– � � � and many more� � �

c� JPK 16

Page 18: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Breadth-first search

The basic strategy of breadth-first search is:

� Mark the current vertex� as “discovered”

� For each edge � � � � in graph � with “undiscovered” successor :

– continue searching “simultaneously” from all such �

– no backtracking

� Mark vertex� as “finished”

this yields the set of vertices that are reachable from the start vertex

c� JPK 17

Page 19: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Breadth-first search – Example

start breadth−first search

done!

explore all next undiscovered vertices

explore all next undiscovered vertices

��

���

���

��

���

���

c� JPK 18

Page 20: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Breadth-first search

void bfsSearch � intlist �� adjV � int � � � � �

int � � � � � � color � queue waiting � // queue of vertices to be processed

for � � � � � � � � color � � � white � // mark all vertices as undiscovered

color � � � � gray; // mark � as discovered but not processed

waiting.Enqueue � � � � // put � in waiting queue

while ��� waiting.isEmpty � � � � // as long as there are unprocessed vertices

� � waiting.Dequeue � � � // take � as next vertex

for each �� in adjV � � � � // check all successors of �

if � color �� � � � white � � // undiscovered vertex found

color �� � � gray; // mark� as not yet processed

waiting.Enqueue �� � � � // put� in waiting queue

color � � � � black; // mark � as finished

c� JPK 19

Page 21: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Properties of breadth-first search

� Vertices are visited in order of increasing distance from start vertex

– after processing all vertices at distance � , vertices are processed at � � �

– search terminates when at distance � no vertices do occur

� Depth of vertex � in breadth-first search tree is its minimum-edgedistance from the start vertex

� Time complexity � � �� �� �� � � ; space complexity � � �� � �

� Vertices to be processed are organized as first-in first-out queue

� A single “processing” opportunity for� : when it is dequeued

c� JPK 20

Page 22: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Depth-first search

The basic strategy of depth-first search is:

� Mark the current vertex� as “discovered”

� For each edge � � � � in graph � with “undiscovered” successor :

– recursively search from �

– i.e., explore edge� � � � � , visit � , explore from there as much as possible– and backtrack from � to �

� For each edge � � � � in graph � with discovered successor :

– “check” edge� � � � � without visiting �

� Mark vertex� as “finished”

this yields the set of vertices that are reachable from the start vertex

c� JPK 21

Page 23: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Depth-first search – Example

explore next vertex

start depth−first search explore next vertex

reached a dead−endbacktrack and explore next vertex

��

���

��

���

��

���

��

���

c� JPK 22

Page 24: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Depth-first search – Example

next state already discoveredbacktrack and explore next vertex

B is a dead−endbacktrack and explore next vertex

D is a dead−endbacktrack and explore next vertex

next state already discoveredbacktrack and explore next vertex

��

���

��

���

��

���

��

���

c� JPK 23

Page 25: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Depth-first search – Example

C already discoveredbacktrack and explore next vertex

explore next vertex

both next vertices already discovered finished

��

���

��

���

��

���

��

���

c� JPK 24

Page 26: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Properties of depth-first search

� Explore a path as far as possible before backtracking

– this corresponds to the order in which recursive calls are made in recursivefunctions

� Time complexity � � �� �� �� � � ; space complexity � � �� � �

� Vertices to be processed are checked in LIFO order

� Two “processing” opportunities for a vertex:

– when the vertex is discovered– when it is marked “finished” (and all its successors are discovered)

� The latter possibility makes depth-first search popular

c� JPK 25

Page 27: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Finding connected components

Problem: find the connected components of undirected graph �

� Construct the corresponding symmetric digraph (with � � �� � edges)

� Use depth-first search:

– start with an arbitrary vertex– do a DFS to find all other vertices (and edges) in the same component– if there are remaining vertices, choose one and repeat

� This yields a depth-first search forest

� This yields a time complexity of � � �� �� �� � �

c� JPK 26

Page 28: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Finding connected components (I)

void connComponents� intlist � � adjV � int � � int � � � � � �

int � � � � � � color �

int � ;

for� � � � � � � � � � � � color � � � white �

for� � � � � � � � � � � �if� color � � �

white �

dfsSearch� adjV � color � � � � � � � � ;

c� JPK 27

Page 29: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Finding connected components (II)

void dfsSearch � intlist �� adjV � int �� color � int � � ccNum � int ���� � � �

int� ;

intlist remAdj � adjV � � � � // successors of �

color � � � � gray;

cc � � � � ccNum � // store component nr of �

while ��� remAdj.isEmpty � � � � // for all successors of �

� � remAdj.first � � � // take� as next vertex

if � color �� � � � white � // undiscovered vertex found

dfsSearch � adjV � color � � � ccNum �� � � � � // recursive search

color � � � � black � ; // processing of � has finished

c� JPK 28

Page 30: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Overview� Graphs

– Graphs, subgraphs, connected graphs, paths, cycles– Representing graphs

� Traversing graphs

– Depth-first search– Breadth-first search

� Directed acyclic graphs

– Topological sorting– Critical path analysis

� Strongly connected components

c� JPK 29

Page 31: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Directed acyclic graphs

Directed acyclic graphs (DAGs) are an important class of graphs:

� Many problems are naturally phrased in terms of a DAG

– scheduling: precedence graphs describe which tasks are to be completedbefore next task can proceed

– a cycle in such a precedence graph means a deadlock

� Many problems have lower complexity on DAGs than on digraphs

� A DAG corresponds to a partial order � on vertices:

– for edge� � � � � this implies � � �

� a partial order is anti-symmetric, and thus cannot contain cycles

� We consider: topological sorting and critical path analysis

c� JPK 30

Page 32: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Topological sorting

� �

� �

� �

� �

� �

� �

which directed graph is acyclic?

c� JPK 31

Page 33: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Topological sorting

� �

� �

� �

��

� �

��

which directed graph is acyclic?

c� JPK 32

Page 34: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Topological sorting

� A topological order for � is:

– an assignment topo of distinct integers � � � � � to the vertices of �

– such that for every edge� � � � � we have topo� � � � topo� � �� topo� � � is called the topological number of �

� If a digraph � has a cycle, then � has no topological order

� Each DAG � has at least one topological order

c� JPK 33

Page 35: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Topological sorting

� �

� �

� �

precedence graphtranspose of “depends on”

Nr Task Depends onA choose clothes I

B dress A, H

C eat breakfast E, F, G

D leave B, C

E make coffee I

F make toast I

G pour juice I

H shower I

I wake up –

does there exist a schedule for this problem?

c� JPK 34

Page 36: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Topological sorting

void dfsSearch � intlist �� adjV � int �� color � int � � topoNum � int �� topo � �

int� ;

intlist remAdj � adjV � � � �color � � � � gray;

while ��� remAdj.isEmpty � � � �

� � remAdj.first � � � // take� as next vertex

if � color �� � � � white � // undiscovered vertex found

dfsSearch � adjV � color � � � topoNum � topo � � � // recursive search

topoNum �

topo � � � � topoNum �

color � � � � black � ; // processing of � has finished

c� JPK 35

Page 37: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Topological sorting

� � � �� � � ��

dependency graph drawn according to the topological order

c� JPK 36

Page 38: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Critical path problem

� A weighted graph is a triple � � � � ��� � where

–� � � � is a graph – directed or undirected – and– � � �� �� � the weight function; � � � is the weight of edge

� Application: determine earliest finish time for set of dependent taskseach with a duration

– earliest start time for task � (est� � � ) is 0 if � has no dependencies;– otherwise est� � � is the maximum of the earliest finish time of its dependencies

� Critical path is a sequence of tasks� � � � � � � � � such that

– � � has no dependencies– � �� � is a dependency of � � such that est� � � � eft� � �� � � “no slack”– eft� � � � is maximum for all tasks

� Problem requires � to be a directed acyclic graph (DAG)

c� JPK 37

Page 39: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Critical path analysis

void dfsSearch � intlist �� adjV � int �� color � int � � int �� duration � critDep � eft � �

int� � est;

intlist remAdj � adjV � � � �color � � � � gray;

est � � � critDep � � � �� � �

while � � remAdj.isEmpty � � � �

� � remAdj.first � � � // take� as next vertex

if � color �� � � � white � // undiscovered vertex found

dfsSearch � adjV � color � � � duration � critDep � eft � �

if � eft �� � � est � � est � eft �� � � critDep � � � � � � �eft � � � � est duration � � � �

color � � � � black � ; // processing of � has finished

c� JPK 38

Page 40: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Critical path analysis

0.0

0.0

0.0

0.0

0.0

3.06.5

6.01.0

done

0.5

2.04.5

8.5

� � � �� � � ��

I A H B E F G C D

wake choose shower dress make make pour eat leaveup clothes coffee toast juice breakf

0.0 3.0 8.5 6.5 4.5 2.0 0.5 6.0 1.0

c� JPK 39

Page 41: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Critical path analysis

0.0

0.0

0.0

0.0

0.0

3.06.5

6.01.0

done

0.5

2.04.5

8.5

� � �������

I A H B E F G C D

wake choose shower dress make make pour eat leaveup clothes coffee toast juice breakf

0.0 3.0 8.5 6.5 4.5 2.0 0.5 6.0 1.0

c� JPK 40

Page 42: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Overview� Graphs

– Graphs, subgraphs, connected graphs, paths, cycles– Representing graphs

� Traversing graphs

– Depth-first search– Breadth-first search

� Directed acyclic graphs

– Topological sorting– Critical path analysis

� Strongly connected components

c� JPK 41

Page 43: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Strongly connected components

� A strong component of digraph � is a maximal strongly connectedsubgraph of �

� Let � � � � � � � � � be the strong components of � . The condensationgraph � � � ��� � ��� � � with:

– ��� is the set of strong components of � , � � ��� � � � � � �� � �

–� � � � � ��� � iff� � and there is� � � � � � with � � � � and � � �

� Strong components and condensation of � and � are stronglyrelated:

– the strong components of � and � � are the same–� � � � � � � � � �

� A vertex in a strong component � is a leader if it is discovered asfirst vertex of � in a DFS

c� JPK 42

Page 44: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

Strongly connected components

strong component

a non-connected digraph and its condensation

c� JPK 43

Page 45: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

A strong component algorithm

Two-phase algorithm to find strong components of digraph � :

� Perform a DFS on graph � putting vertices on a stack at their finishingtime

� Perform a DFS on the transposed graph �

– take white vertices from the stack constructed in phase 1– store the leader of vertex � ’s strong component (scc � � � )

� Each DFS tree generated in phase 2 is exactly one strong component

� Time complexity is � � �� �� �� � � :– each depth-first search takes � � �

��

��

� �

– constructing the transposed graph takes � � �

��

��

� � exercise

c� JPK 44

Page 46: Elementary Graph Algorithms - FMTfmt.cs.utwente.nl/courses/adc/lec6.pdf · Elementary Graph Algorithms Lecture #6 of Algorithms, Data structures and Complexity ... Traversal strategies

#6: Elementary Graph Algorithms ADC (214020)

A strong component algorithm

EGAFBDC

original digraph its transpose

stack at end phase 1detected strong components in phase 2

��

��

��

���

��

��

c� JPK 45