124
Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora U¸ car GRAAL, LIP, ENS Lyon, France CR-07: Sparse Matrix Computations, September 2010 http://graal.ens-lyon.fr/ ~ bucar/CR07/ 1/124 CR09

Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Embed Size (px)

Citation preview

Page 1: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Outline

Introduction tograph theory and algorithms

Jean-Yves L’Excellent and Bora Ucar

GRAAL, LIP, ENS Lyon, France

CR-07: Sparse Matrix Computations, September 2010http://graal.ens-lyon.fr/~bucar/CR07/

1/124 CR09

Page 2: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Outline

Outline

1 Definitions and some problems

2 Basic algorithmsBreadth-first searchDepth-first searchTopological sortStrongly connected components

3 Questions

2/124 CR09

Page 3: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Graph notations and definitions

A graph G = (V ,E ) consists of a finite set V , called the vertex set and afinite, binary relation E on V , called the edge set.

Three standard graph models

Undirected graph: The edges are unordered pair of vertices, i.e.,{u, v} ∈ E for some u, v ∈ V .

Directed graph: The edges are ordered pair of vertices, that is, (u, v) and(v , u) are two different edges.

Bipartite graph: G = (U ∪ V ,E ) consists of two disjoint vertex sets Uand V such that for each edge (u, v) ∈ E , u ∈ U and v ∈ V .

An ordering or labelling of G = (V ,E ) having n vertices, i.e., |V | = n, isa mapping of V onto 1, 2, . . . , n.

3/124 CR09

Page 4: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Matrices and graphs: Rectangular matrices

The rows/columns and nonzeros of a given sparse matrix correspond (withnatural labelling) to the vertices and edges, respectively, of a graph.

Rectangular matrices

A =

0@1 2 3 4

1 × ×2 × ×3 × ×

1A

Bipartite graph

1

r2

r3

1c

2c

3c

4c

r

The set of rows corresponds to one of the vertex set R, the set of columns

corresponds to the other vertex set C such that for each aij 6= 0, (ri , cj) is an

edge.

4/124 CR09

Page 5: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Matrices and graphs: Square unsymmetric pattern

The rows/columns and nonzeros of a given sparse matrix correspond (withnatural labelling) to the vertices and edges, respectively, of a graph.

Square unsymmetricpattern matrices

A =

0@1 2 3

1 × ×2 × ×3 ×

1A

Graph models

Bipartite graph as before.

Directed graph

v2 3v

1v

The set of rows/cols corresponds the vertex set V such that for each aij 6= 0,

(vi , vj) is an edge. Transposed view possible too, i.e., the edge (vi , vj) directed

from column i to row j . Usually self-loops are omitted.5/124 CR09

Page 6: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Matrices and graphs: Square unsymmetric pattern

A special subclass

Directed acyclic graphs (DAG):A directed graphs with no loops(maybe except for self-loops).

DAGs

We can sort the vertices such thatif (u, v) is an edge, then u appearsbefore v in the ordering.

Question: What kind of matrices have a DAG?

6/124 CR09

Page 7: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Matrices and graphs: Symmetric pattern

The rows/columns and nonzeros of a given sparse matrix correspond (withnatural labelling) to the vertices and edges, respectively, of a graph.

Square symmetricpattern matrices

A =

0@1 2 3

1 ×2 × × ×3 × ×

1A

Graph models

Bipartite and directed graphs as before.

Undirected graph

v2 3v

1v

The set of rows/cols corresponds the vertex set V such that for each

aij , aji 6= 0, {vi , vj} is an edge. No self-loops; usually the main diagonal is

assumed to be zero-free.7/124 CR09

Page 8: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Definitions: Edges, degrees, and paths

Many definitions for directed and undirected graphs are the same. Wewill use (u, v) to refer to an edge of an undirected or directed graph toavoid repeated definitions.

An edge (u, v) is said to incident on the vertices u and v .

For any vertex u, the set of vertices in adj(u) = {v : (u, v) ∈ E} arecalled the neighbors of u. The vertices in adj(u) are said to beadjacent to u.

The degree of a vertex is the number of edges incident on it.

A path p of length k is a sequence of vertices 〈v0, v1, . . . , vk〉 where(vi−1, vi ) ∈ E for i = 1, . . . , k . The two end points v0 and vk aresaid to be connected by the path p, and the vertex vk is said to bereachable from v0.

8/124 CR09

Page 9: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Definitions: Components

An undirected graph is said to be connected if every pair of verticesis connected by a path.

The connected components of an undirected graph are theequivalence classes of vertices under the “is reachable” fromrelation.

A directed graph is said to be strongly connected if every pair ofvertices are reachable from each other.

The strongly connected components of a directed graph are theequivalence classes of vertices under the “are mutually reachable”relation.

9/124 CR09

Page 10: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Definitions: Trees and spanning trees

A tree is a connected, acyclic, undirected graph. If an undirected graph isacyclic but disconnected, then it is a forest.

Properties of trees

Any two vertices are connected by a unique path.

|E | = |V | − 1

A rooted tree is a tree with a distinguished vertex r , called the root.

There is a unique path from the root r to every other vertex v . Anyvertex y in that path is called an ancestor of v . If y is an ancestor of v ,then v is a descendant of y .

The subtree rooted at v is the tree induced by the descendants of v ,rooted at v .

A spanning tree of a connected graph G = (V ,E ) is a tree T = (V ,F ),such that F ⊆ E .

10/124 CR09

Page 11: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Ordering of the vertices of a rooted tree

A topological ordering of a rooted tree is an ordering that numberschildren vertices before their parent.

A postorder is a topological ordering which numbers the vertices inany subtree consecutively.

u w

x

y

z

v

with topological ordering

1 3

2

4

5

6

Rooted spanning tree

w

yz

xu

v

Connected graph G

u w

x

y

z

v

1

6

54

3

2

Rooted spanning treewith postordering

11/124 CR09

Page 12: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Postordering the vertices of a rooted tree – I

The following recursive algorithm will do the job:

[porder ]=PostOrder(T , r)

for each child c of r doporder ← [porder , PostOrder(T , c)]

porder ← [porder , r ]

We need to run the algorithm for each root r when T is a forest.

Usually recursive algorithms are avoided, as for a tree with large numberof vertices can cause stack overflow.

12/124 CR09

Page 13: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Postordering the vertices of a rooted tree – II

[porder ]=PostOrder(T , r)

porder ← [·]seen(v)← False for all v ∈ Tseen(r)← TruePush(S , r)while NotEmpty(S) do

v ←Pop(S)if ∃ a child c of v with seen(c) = False then

seen(c)← TruePush(S , c)

elseporder ← [porder , v ]

Again, have to run for each root, if T is a forest.

Both algorithms run in O(n) time for a tree with n nodes.

13/124 CR09

Page 14: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Permutation matrices

A permutation matrix is a square (0, 1)-matrix where each row andcolumn has a single 1.

If P is a permutation matrix, PPT = I , i.e., it is an orthogonal matrix.Let,

A =

1 2 3

1 × ×2 × ×3 ×

and suppose we want to permute columns as [2, 1, 3]. Define p2,1 = 1,p1,2 = 1, p3,3 = 1, and B = AP (if column j to be at position i , setpji = 1)

B =

2 1 3

1 × ×2 × ×3 ×

=

1 2 3

1 × ×2 × ×3 ×

1 2 3

1 12 13 1

14/124 CR09

Page 15: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Matching in bipartite graphs and permutations

A matching in a graph is a set of edges no two of which share a commonvertex. We will be mostly dealing with matchings in bipartite graphs.

In matrix terms, a matching in the bipartite graph of a matrixcorresponds to a set of nonzero entries no two of which are in the samerow or column.

A vertex is said to be matched if there is an edge in the matchingincident on the vertex, and to be unmatched otherwise. In a perfectmatching, all vertices are matched.

The cardinality of a matching is the number of edges in it. A maximumcardinality matching or a maximum matching is a matching of maximumcardinality. Solvable in polynomial time.

15/124 CR09

Page 16: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Matching in bipartite graphs and permutations

Given a square matrix whose bipartite graph has a perfect matching, sucha matching can be used to permute the matrix such that the matchingentries are along the main diagonal.

1

r2

r3 3c

2c

1cr 1 2 3

1 × ×2 × ×3 ×

2 1 3

1 × ×2 × ×3 ×

=

1 2 3

1 × ×2 × ×3 ×

1 2 3

1 12 13 1

16/124 CR09

Page 17: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Definitions: Reducibility

Reducible matrix: An n × n square matrix is reducible if there exists ann × n permutation matrix P such that

PAPT =

(A11 A12

O A22

),

where A11 is an r × r submatrix, A22 is an (n − r)× (n − r) submatrix,where 1 ≤ r < n.

Irreducible matrix: There is no such a permutation matrix.

Theorem: An n × n square matrix is irreducible iff its directed graph isstrongly connected.

Proof: Follows by definition.

17/124 CR09

Page 18: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Definitions: Fully indecomposability

Fully indecomposable matrix: There is no permutation matrices P and Qsuch that

PAQ =

(A11 A12

O A22

),

with the same condition on the blocks and their sizes as above.

Theorem: An n × n square matrix A is fully indecomposable iff for somepermutation matrix P, the matrix PA is irreducible and has a zero-freemain diagonal.

Proof: We will come later in the semester to the “if” part.

Only if part (by contradiction): Let B = PA be an irreducible matrix withzero-free main diagonal. B is fully indecomposable iff A is (why?).Therefore we may assume that A is irreducible and has a zero-freediagonal. Suppose, for the sake of contradiction, A is not fullyindecomposable.

18/124 CR09

Page 19: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Fully indecomposable matrices

Fully indecomposable matrix

There is no permutation matrices P and Q such that

PAQ =

(A11 A12

O A22

),

with the same condition on the blocks and their sizes as above.

Proof cont.: Let P1AQ1 be of the form above with A11 of size r × r . Wemay write P1AQ1 = A′Q ′, where A′ = P1AP1

T with zero-free diagonal(why?), and Q ′ = P1Q1 is a permutation matrix which has to permute(why?) the first r columns among themselves, and similarly the last n− rcolumns among themselves. Hence, A′ is in the above form, and A isreducible: contradiction. 2

19/124 CR09

Page 20: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Definitions: Cliques and independent sets

Clique

In an undirected graph G = (V , E), a set of vertices S ⊆ V is a clique if for alls, t ∈ S , we have (s, t) ∈ E .

Maximum clique: A clique of maximum cardinality (finding a maximum cliquein an undirected graph is NP-complete).

Maximal clique: A clique is a maximal clique, if it is not contained in anotherclique.

In a symmetric matrix A, a clique corresponds to a subset of rows R and thecorresponding columns such that the matrix A(R, R) is full.

Independent set

A set of vertices is an independent set if none of the vertices are adjacent toeach other. Can we find the largest one in polynomial time?

In a symmetric matrix A, an independent set corresponds to a subset of rows Rand the corresponding columns such that the matrix A(R, R) is either zero, ordiagonal.

20/124 CR09

Page 21: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Definitions: More on cliques

Clique: In an undirected graph G = (V ,E ), a set of vertices S ⊆ V is aclique if for all s, t ∈ S , we have (s, t) ∈ E .

In a symmetric matrix A corresponds to a subset of rows R and thecorresponding columns such that the matrix A(R,R) is full.

Cliques in bipartite graphs: Bi-cliques

In a bipartite graph G = (U ∪ V ,E ), a pair of sets 〈R,C 〉 where R ⊆ Uand C ⊆ V is a bi-clique if for all a ∈ R and b ∈ C , we have (a, b) ∈ E .

In a matrix A, corresponds to a subset of rows R and a subset of columnsC such that the matrix A(R,C ) is full.

The maximum node bi-clique problem asks for a bi-clique of maximumsize (e.g., |R|+ |C |), and it is polynomial time solvable, whereasmaximum edge bi-clique problem (e.g., asks for a maximum |R| × |C |) isNP-complete.

21/124 CR09

Page 22: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Definitions: Hypergraphs

Hypergraph: A hypergraph H = (V , N) consists of a finite set V called thevertex set and a set of non-empty subsets of vertices N called the hyperedgeset or the net set. A generalization of graphs.

For a matrix A, define a hypergraph whose vertices correspond to the rows andwhose nets correspond to the columns such that vertex vi is in net nj iff aij 6= 0(the column-net model).

A sample matrix

0@1 2 3 4

1 × × ×2 × ×3 × × ×

1AThe column-net hypergraph model

43n

1n

1v

2v

3v

n

n2v

2n

4n

3n

1n

1

2

3

v

v

22/124 CR09

Page 23: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Basic graph algorithms

Searching a graph: Systematically following the edges of the graph so asto visit all the vertices.

Breadth-first search,

Depth-first search.

Topological sort (of a directed acyclic graph): It is a linear ordering of allthe vertices such that if (u, v) directed is an edge, then u appears beforev in the ordering.

Strongly connected components (of a directed graph; why?): Recall thata strongly connected component is a maximal set of vertices for whichevery pair its vertices are reachable. We want to find them all.

We will use some of the course notes by Cevdet Aykanat(http://www.cs.bilkent.edu.tr/~aykanat/teaching.html)

23/124 CR09

Page 24: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Idea

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

2

Graph Searching: Breadth-First Search

Graph G =(V, E), directed or undirected with adjacency list repres.

GOAL: Systematically explores edges of G to

• discover every vertex reachable from the source vertex s

• compute the shortest path distance of every vertex from the source vertex s

• produce a breadth-first tree (BFT) G! with root s

" BFT contains all vertices reachable from s

" the unique path from any vertex v to s in G!

constitutes a shortest path from s to v in G

IDEA: Expanding frontier across the breadth -greedy-

• propagate a wave 1 edge-distance at a time

• using a FIFO queue: O(1) time to update pointers to both ends

24/124 CR09

Page 25: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Key components

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

3

Breadth-First Search Algorithm

Maintains the following fields for each u ! V

• color[u]: color of u

"WHITE : not discovered yet

"GRAY : discovered and to be or being processed

" BLACK: discovered and processed

• #[u]: parent of u (NIL of u = s or u is not discovered yet)

• d[u]: distance of u from s

Processing a vertex = scanning its adjacency list

25/124 CR09

Page 26: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Algorithm

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

4

Breadth-First Search Algorithm

BFS(G, s)

for each u ! V" {s} docolor[u] # WHITE$[u] # NIL; d [u] #%

color[s] # GRAY$[s] # NIL; d [s] # 0Q # {s}while Q & ' do

u # head[Q]for each v in Adj[u] do

if color[v] = WHITE thencolor[v] # GRAY$[v] # ud [v] # d [u] + 1ENQUEUE(Q, v)

DEQUEUE(Q)color[u] # BLACK

26/124 CR09

Page 27: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Example

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

5

Breadth-First Search

Sample Graph:

a

b

g

c

f

d

e

h

i

0

s FIFO just after

queue Q processing vertex

!a" -

27/124 CR09

Page 28: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Example

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

6

Breadth-First Search

a

b

g

c

f

d

e

h

i

0

1

1

s FIFO just after

queue Q processing vertex

!a" -

!a,b,c" a

28/124 CR09

Page 29: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Example

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

7

Breadth-First Search

a

b

g

c

f

d

e

h

i

0

1

1

2

s FIFO just after

queue Q processing vertex

!a" -

!a,b,c" a

!a,b,c,f" b

29/124 CR09

Page 30: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Example

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

8

Breadth-First Search

a

b

g

c

f

d

e

h

i

0

1

1

2

2

s FIFO just after

queue Q processing vertex

!a" -

!a,b,c" a

!a,b,c,f" b

!a,b,c,f,e" c

30/124 CR09

Page 31: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Example

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

9

Breadth-First Search

a

b

g

c

f

d

e

h

i

0

1

1

2

2

s

3 3

FIFO just after

queue Q processing vertex

!a" -

!a,b,c" a

!a,b,c,f" b

!a,b,c,f,e" c

!a,b,c,f,e,g,h" f

31/124 CR09

Page 32: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Example

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

10

Breadth-First Search

a

b

g

c

f

d

e

h

i

0

1

1

2

2

s

3 3

3

3

FIFO just after

queue Q processing vertex

!a" -

!a,b,c" a

!a,b,c,f" b

!a,b,c,f,e" c

!a,b,c,f,e,g,h" f

!a,b,c,f,e,g,h,d,i" e

all distances are filled in after processing e

32/124 CR09

Page 33: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Example

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

11

Breadth-First Search

a

b

g

c

f

d

e

h

i

0

1

1

2

2

s

3 3

3

3

FIFO just after

queue Q processing vertex

!a" -

!a,b,c" a

!a,b,c,f" b

!a,b,c,f,e" c

!a,b,c,f,e,g,h" f

!a,b,c,f,e,g,h,d,i" g

33/124 CR09

Page 34: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Example

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

12

Breadth-First Search

a

b

g

c

f

d

e

h

i

0

1

1

2

2

s

3 3

3

3

FIFO just after

queue Q processing vertex

!a" -

!a,b,c" a

!a,b,c,f" b

!a,b,c,f,e" c

!a,b,c,f,e,g,h" f

!a,b,c,f,e,g,h,d,i" h

34/124 CR09

Page 35: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Example

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

13

Breadth-First Search

a

b

g

c

f

d

e

h

i

0

1

1

2

2

s

3 3

3

3

FIFO just after

queue Q processing vertex

!a" -

!a,b,c" a

!a,b,c,f" b

!a,b,c,f,e" c

!a,b,c,f,e,g,h" f

!a,b,c,f,e,g,h,d,i" d

35/124 CR09

Page 36: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Example

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

14

Breadth-First Search

a

b

g

c

f

d

e

h

i

0

1

1

2

2

s

3 3

3

3

FIFO just after

queue Q processing vertex

!a" -

!a,b,c" a

!a,b,c,f" b

!a,b,c,f,e" c

!a,b,c,f,e,g,h" f

!a,b,c,f,e,g,h,d,i" i

algorithm terminates: all vertices are processed

36/124 CR09

Page 37: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: Analysis

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

15

Breadth-First Search Algorithm

Running time: O(V+E) = considered linear time in graphs

• initialization: !(V)

• queue operations: O(V)

" each vertex enqueued and dequeued at most once

" both enqueue and dequeue operations take O(1) time

• processing gray vertices: O(E)

" each vertex is processed at most once and

!#

!=Vu

EuAdj )(|][|

37/124 CR09

Page 38: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: The paths to the root

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

25

Breadth-First Tree Generated by BFS

LEMMA 4: predecessor subgraph G!=(V!, E!) generated by BFS(G, s) , where V! ={v " V: ![v] # NIL}${s} and

E! ={(![v],v) " E: v " V! %{s}}is a breadth-first tree such that

% V! consists of all vertices in V that are reachable from s

% &v " V! , unique path p(v, s) in G! constitutes a sp(s, v) in G

PRINT-PATH(G, s, v)

if v = s then print selse if ![v] = NIL then

print no “s!v path”else

PRINT-PATH(G, s, ![v] )print v

Prints out vertices on a

s!v shortest path

38/124 CR09

Page 39: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Breadth-first search: The BFS tree

CS473 – Lecture 14 Cevdet Aykanat - Bilkent University

Computer Engineering Department

26

Breadth-First Tree Generated by BFS

a

b

g

c

f

d

e

h

i

0

1

1

2

2

s

3 3

3

3

a

b

g

c

f

d

e

h

i

0

1

1

2

2

s

3 3

3

3

BFS(G,a) terminated BFT generated by BFS(G,a)

39/124 CR09

Page 40: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Idea

!"#$%& '()*+,(#-$ .

/(0*1234,5*#"(6,)1

7 8,601#8!9:;<=#>4,()*(>#?,#+@>4,()*(>

7 A>B6)(@)C#D45*#,(0,(5(@*6*4?@

7 8?6DE#"C5*(F6*4)6DDC#(G0D?,(#(H(,C#H(,*(G#6@>#

(H(,C#(>I(

7 J>(6E#5(6,)1#>((0(,#K1(@(H(,#0?554LD(

M N54@I#6#'J3O#P+(+(#9"*6)QR#3J3O#P+(+(#+5(>#4@#S3"=

40/124 CR09

Page 41: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Key components

!"#$%& '()*+,(#-$ &

.(/*0123,4*#"(5,)0

6 7538*5384#4(9(,5:#;3(:<4#;=,#(5)0#!!>

6 '3?(#@2"A#)=:=,4 *0(#9(,*3)(4#*=#38<3)5*(#*0(3,#

4*5*(4B#C5)0#9(,*(D#34

E F83*35::G#H03*(A#

E I,5G(<#H0(8#<34)=9(,(<A#

E J:5)?(8(<#H0(8#;38340(<

6 '3?(#@2"A#,()=,<4#<34)=9(,G =;#5#H03*(#! <+,38I#

4)58838I#K<LM"N#JG#"M!N# "

41/124 CR09

Page 42: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Key components

!"#$%& '()*+,(#-$ $

.(/*0123,4*#"(5,)0

6 7893:(#;2"<#/,(=()(44>,#?,5/0#@!/,>=+)(=#AB#

.2"#C>,D4#4/58838?#C>,(4*

6 @!"EF<G

!H#I0(,(#

G!"JE!K!L<!HM#!#F#58= !K!L$ NO'P

6 @!" =(/*01C3,4*#C>,(4*#E.22H#34#)>D/>4(=#>C#

=34Q>38*#=(/*01C3,4*#*,((4#E.2R4H

42/124 CR09

Page 43: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Key components

!"#$%& '()*+,(#-$ .

/(0*1234,5*#"(6,)1

7 /3"#6859#*4:(5*6:05#(6)1#;(,*(<#=4*1#*=9#*4:(5*6:05

7 >?!@A#,()9,>5#=1(B#! 45#C4,5*#>45)9;(,(>#6B>#D,6E(>

7 C?!@A#,()9,>5#=1(B#! 45#C4B451(>#6B>#F86)G(B(>

7 "4B)(#*1(,(#45#9B8E#9B(#>45)9;(,E#(;(B*#6B>#C4B4514BD#

(;(B*#C9,#(6)1#;(,*(<#=(#16;(#-! >?!@#" C?!@! HIJI

- H >?!@ C?!@ HIJI

K4:(#6<45#C9,#*1(#)989,#9C#6#;(,*(<

43/124 CR09

Page 44: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Algorithm

!"#$%& '()*+,(#-$ .

/(0*1234,5*#"(6,)1

!"#789

$%&'()*+ !!:#,%

);<;,=!>" "#$%&#=!>#" ?@'

%$'&" A

$%&'()*+ !!:#,%

-$');<;,=!> $ "#$%& .+(/

!"#012#23485'!6

!"#012#23485'!6

);<;,=!>" ()*+

B=!>" %$'&" %$'& %-

$%&'()*+ ,! CBD=!>#,%

-$');<;,=,> $ "#$%& .+(/#=,>#" !!"#012#23485',6

);<;,=!>" -.*/0

E=!>" %$'&" %$'& %-

44/124 CR09

Page 45: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Analysis

!"#$%& '()*+,(#-$ %

.(/*0123,4*#"(5,)0

6 7+88389#*3:(;#!<=">?

6 @83*35A3B5*3C8#ACC/#38#!"# ;#!<=?

6 D538#ACC/#38#!"#;#!<=?#(E)A+43F(#CG#*3:(#*C#(E()+*(#)5AA4#*C#!"#$%&#&'(

6 !"#$%&#&'(34#)5AA(H#(E5)*AI#C8)(#GC,#(5)0#!#=#438)(

J !"#$%&#&'(34#38FCK(H#C8AI#C8#L03*(#F(,*3)(4#58H

J !"#$%&#&')M*("+ 3::(H35*(AI#)CAC,4#+#54#9,5I

6 2C,#ACC/#CG#!"#$%&#&')M*("+ 34#(E()+*(H#NOHPQ"RN#*3:(

6 "38)(#$ NOHPQ"RN#% >S#*C*5A#)C4*#CG#(E()+*389#ACC/#CG#

!"#$%&#&'(34#!<>?

45/124 CR09

Page 46: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ .

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

46/124 CR09

Page 47: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ .

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

47/124 CR09

Page 48: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ -.

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

"

48/124 CR09

Page 49: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ --

.(/*0123,4*#"(5,)06#7859/:(

! " #

$ %

& ' (

!

"

#

49/124 CR09

Page 50: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ -.

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

"

#

50/124 CR09

Page 51: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ -&

.(/*0123,4*#"(5,)06#7859/:(

! " #

$ %

& ' (

!

"

# $

51/124 CR09

Page 52: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ -$

.(/*0123,4*#"(5,)06#7859/:(

! " #

$ %

& ' (

!

"

# $ %

52/124 CR09

Page 53: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ -.

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

"

# $ %

53/124 CR09

Page 54: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ -.

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

"

# $ % &

54/124 CR09

Page 55: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ -%

.(/*0123,4*#"(5,)06#7859/:(

! " #

$ %

& ' (

!

"

# $ % &

'

55/124 CR09

Page 56: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ -.

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

"

# $ % &

'

(

56/124 CR09

Page 57: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ -.

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

"

# $ % &

'

(

57/124 CR09

Page 58: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ ./

0(1*2345,6*#"(7,)28#9:7;1<(

! " #

$ %

& ' (

!

"

# $ % &

'

(

)

58/124 CR09

Page 59: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ .-

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

"

# $ % &

'

(

)

59/124 CR09

Page 60: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ ..

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

"

# $ % &

'

(

) !*

60/124 CR09

Page 61: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ .&

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

"

# $ % &

' ( !)

* !!

61/124 CR09

Page 62: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ .$

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

" # $ %

& ' ()

* ((( (!

62/124 CR09

Page 63: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ ./

0(1*2345,6*#"(7,)28#9:7;1<(

! " #

$ %

& ' (

!

" # $ %

& ' ()

* ((( (! ("

63/124 CR09

Page 64: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ ./

0(1*2345,6*#"(7,)28#9:7;1<(

! " #

$ %

& ' (

!

" # $ %

& ' ()

* ((( (! ("

64/124 CR09

Page 65: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ .%

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

" # $ %

& ' ()

* ((( (! ("

65/124 CR09

Page 66: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ ./

0(1*2345,6*#"(7,)28#9:7;1<(

! " #

$ %

& ' (

!

" # $ %

& ' ()

* ((( (! ("

(#

66/124 CR09

Page 67: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ ./

0(1*2345,6*#"(7,)28#9:7;1<(

! " #

$ %

& ' (

!

" # $ %

& ' ()

* ((( (! ("

(#

67/124 CR09

Page 68: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ &.

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

" # $ %

& ' ()

* ((( (! ("

(# ($

68/124 CR09

Page 69: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Example

!"#$%& '()*+,(#-$ &-

.(/*0123,4*#"(5,)06#7859/:(

! " #

$ %

& ' (

!

" # $ %

& ' ()

* ((( (!

(# ($

(" (%

69/124 CR09

Page 70: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: DFT and DFF

!"#$%& '()*+,(#-$ &.

/(0*1234,5*#"(6,)17#896:0;(

! " #

$ %

& ' (

!

" # $ %

& ' ()

* ((( (!

(# ($

(" (%

! " #

$ %

& ' (

!

" # $ %

& ' ()

* ((( (!

(# ($

(" (%

/3"<=>#*(,:4?6*(@ /(0*12A4,5*#AB,(5*#</33>

70/124 CR09

Page 71: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Parenthesis theorem

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

2

DFS: Parenthesis Theorem

Thm: In any DFS of G=(V,E), let int[v] = [d[v], f[v]]

then exactly one of the following holds

for any u and v !V

• int[u] and int[v] are entirely disjoint

• int[v] is entirely contained in int[u] and

v is a descendant of u in a DFT

• int[u] is entirely contained in int[v] and

u is a descendant of v in a DFT

71/124 CR09

Page 72: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Parenthesis theorem

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

5

Parenthesis

Theorem

x y z

s t

w v u

2

3 4 5 6

7 9 1 0

8 1 11 1 2

1 4 1 5

1 3 1 6

1 2 3 4 5 6 7 8 9

x z

s y u

w v t

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

1 0 1 1 1 2 1 3 1 4 1 5 1 6

( x ( s ( w w ) ( v v ) s ) ( y ( t t ) y ) x ) ( z ( u u ) z )

72/124 CR09

Page 73: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

6

Edge Classification in a DFF

Tree Edge: discover a new (WHITE) vertex!GRAY to WHITE"

Back Edge: from a descendent to an ancestor in DFT!GRAY to GRAY"

Forward Edge: from ancestor to descendent in DFT!GRAY to BLACK"

Cross Edge: remaining edges (btwn trees and subtrees)!GRAY to BLACK"

Note: ancestor/descendent is wrt Tree Edges

73/124 CR09

Page 74: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

7

Edge Classification in a DFF

• How to decide which GRAY to BLACK edges are forward, which are cross

Let BLACK vertex v !Adj[u] is encountered while processing GRAY vertex u

– (u,v) is a forward edge if d[u] <<<< d[v]

– (u,v) is a cross edge if d[u] >>>> d[v]

74/124 CR09

Page 75: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

8

Depth-First Search: Example

x y z

s t

w v u

1

75/124 CR09

Page 76: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

9

Depth-First Search: Example

x y z

s t

w v u

1

2

T

76/124 CR09

Page 77: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

10

Depth-First Search: Example

x y z

s t

w v u

1

2

3

T

T

77/124 CR09

Page 78: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

11

Depth-First Search: Example

x y z

s t

w v u

1

2

3

T

T

B

78/124 CR09

Page 79: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

12

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4

T

T

B

79/124 CR09

Page 80: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

13

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5

T

T

T

B

80/124 CR09

Page 81: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

14

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5

T

T

T

B

C

81/124 CR09

Page 82: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

15

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

T

T

T

B

C

82/124 CR09

Page 83: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

16

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

T

T

T

B

C

83/124 CR09

Page 84: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

17

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

8T

T

T

T

B

C

84/124 CR09

Page 85: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

18

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

8T

T

T

T

B

C

C

85/124 CR09

Page 86: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

19

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

8

9

T

T

T

T

T

B

C

C

86/124 CR09

Page 87: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

20

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

8

9

T

T

TT

T

C

B C

C

87/124 CR09

Page 88: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

21

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

8

9 10

T

T

T

T

T

B

C

C

C

88/124 CR09

Page 89: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

22

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7 9 10

8 11

T

T

T

T

T

B

C

C

C

F

89/124 CR09

Page 90: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

23

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12T

T

TT

T

BF

C

C

C

90/124 CR09

Page 91: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

24

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12 13

T

T

T

T

T

B C

F

C

C

91/124 CR09

Page 92: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

25

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12 13

T

T

T

T

T

C

BF

C

C

C

92/124 CR09

Page 93: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

26

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12 13

T

T

T

TT

B

C

F

C

C

C

C

93/124 CR09

Page 94: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

27

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12 13

14

T

T

T

T

T

B

F

C

CC

C

C

T

94/124 CR09

Page 95: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

28

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12 13

14

C

T

T

T

T

T

T

B

F

C

C

C

C

C

95/124 CR09

Page 96: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Edge classification example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

29

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12

14 15

13 16T

T

T

TT

T

C

F

B C

C

C

C

C

96/124 CR09

Page 97: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Undirected graphs

Edge classification

Any DFS on an undirected graph produces only Tree and Back edges.

16/17

a c g

h f

d j

i e b

1/20

2/19

3/12

5/86/7

9/10

4/11

13/14

15/18

i

j

h

g

c f

e d

a

b

97/124 CR09

Page 98: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Depth-first search: Non-recursive algorithm

[π, d , f ]=DFS(G , v)

top ← 1stack(top)← vd(v)← ctime ← 1while top > 0 do

u ← stack(top)if there is a vertex w ∈ Adj(u) where π(w) is not set then

top ← top + 1stack(top)← wπ(w)← ud(w)← ctime ← ctime + 1

elsef (u)← ctime ← ctime + 1top ← top − 1

98/124 CR09

Page 99: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Topological sort

Topological sort (of a directed acyclic graph): It is a linear ordering of allthe vertices such that if (u, v) is a directed edge, then u appears before vin the ordering.

Ordering is not necessarily unique.

99/124 CR09

Page 100: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Topological sort: Example

3/4

short socks watch

pants shoes shirt

tiebelt jacket

pantsundershortsocks shoes watch shirt belt tie jacket

6/7 3/4

11/16 17/18 9/10

13/14

2/5

1/812/15

17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5

under

100/124 CR09

Page 101: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Topological sort: Algorithm

The algorithm

run DFS(G )

when a vertex is finished, output it

vertices are output in the reverse topologically sorted order

Runs in O(V + E ) time — a linear time algorithm.

The algorithm: Correctness

if (u, v) ∈ E , then f [u] > f [v ]

Proof: Consider the color of v during exploring the edge (u, v), where uis Gray. 2

v cannot be Gray (otherwise a Back edge in an acyclic graph !!!).

If v is White, then u is an ancestor of v , hence f [u] > f [v ].

If v is Black, f [v ] is computed already, f [u] is going to be computed,hence f [u] > f [v ].

101/124 CR09

Page 102: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components (SCC)

The strongly connected components of a directed graph are theequivalence classes of vertices under the “are mutually reachable”relation.

For a graph G = (V ,E ), the transpose is defined as GT = (V ,ET ),where ET = {(u, v) : (v , u) ∈ E}.

Constructing GT from G takes O(V + E ) time with adjacency list (likethe CSR or CSC storage format for sparse matrices) representation.

Notice that G and GT have the same SCCs.

102/124 CR09

Page 103: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Algorithm

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

3

Strongly Connected Components

Algorithm

(1) Run DFS(G) to compute finishing times for all u!V

(2) Compute GT

(3) Call DFS(GT) processing vertices in main loop in

decreasing f[u] computed in Step (1)

(4) Output vertices of each DFT in DFF of Step (3) as a

separate SCC

103/124 CR09

Page 104: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Analysis

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

4

Strongly Connected Components

Lemma 1: no path between a pair of vertices in

the same SCC, ever leaves the SCC

Proof: let u and v be in the same SCC ! u ! v

let w be on some path u !w ! v ! u !w

but v ! u ! ! a path w !v ! u ! w !u

therefore u and w are in the same SCC

QED

u

v

wSCC

104/124 CR09

Page 105: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

5

SCC: Example

a b c

e

d

f g h

105/124 CR09

Page 106: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

6

SCC: Example

a b c

e

d

f g h

1

1

(1)Run DFS(G) to compute finishing times for all u!V

106/124 CR09

Page 107: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

7

SCC: Example

a b c

e

d

f g h

1

1

10

2 73 4 5 6

8 9

(1)Run DFS(G) to compute finishing times for all u!V

107/124 CR09

Page 108: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

8

SCC: Example

a b c

e

d

f g h

1

2

10

2 73 4 5 6

8 911

(1)Run DFS(G) to compute finishing times for all u!V

108/124 CR09

Page 109: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

9

SCC: Example

a b c

e

d

f g h

1

2

10

2 73 4 5 6

8 916111413

1512

1

Vertices sorted according to the finishing times:

!!!!b, e, a, c, d, g, h, f """"

109/124 CR09

Page 110: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

10

SCC: Example

a b c

e

d

f g h

(2)Compute GT

110/124 CR09

Page 111: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

11

SCC: Example

a b c

e

d

f g h

(3) Call DFS(GT) processing vertices in main loop in

decreasing f[u] order: !!!!b, e, a, c, d, g, h, f """"

111/124 CR09

Page 112: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

12

SCC: Example

a b c

e

d

f g h

r1=

(3) Call DFS(GT) processing vertices in main loop in

decreasing f[u] order: !!!!b, e, a, c, d, g, h, f """"

112/124 CR09

Page 113: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

13

SCC: Example

a b c

e

d

f g h

r1=

(3) Call DFS(GT) processing vertices in main loop in

decreasing f[u] order: !!!!b, e, a, c, d, g, h, f """"

113/124 CR09

Page 114: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

14

SCC: Example

a b c

e

d

f g h

r1= r

2=

(3) Call DFS(GT) processing vertices in main loop in

decreasing f[u] order: !!!!b, e, a, c, d, g, h, f """"

114/124 CR09

Page 115: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

15

SCC: Example

a b c

e

d

f g h

r1= r

2=

(3) Call DFS(GT) processing vertices in main loop in

decreasing f[u] order: !!!!b, e, a, c, d, g, h, f """"

115/124 CR09

Page 116: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

16

SCC: Example

a b c

e

d

f g h

r1= r

2=

r3=

(3) Call DFS(GT) processing vertices in main loop in

decreasing f[u] order: !!!!b, e, a, c, d, g, h, f """"

116/124 CR09

Page 117: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

17

SCC: Example

a b c

e

d

f g h

r1= r

2=

r3=

(3) Call DFS(GT) processing vertices in main loop in

decreasing f[u] order: !!!!b, e, a, c, d, g, h, f """"

117/124 CR09

Page 118: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

18

SCC: Example

a b c

e

d

f g h

r1= r

2=

r3= r

4=

(3) Call DFS(GT) processing vertices in main loop in

decreasing f[u] order: !!!!b, e, a, c, d, g, h, f """"

118/124 CR09

Page 119: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

19

SCC: Example

a b c

e

d

f g h

r1= r

2=

r3= r

4=

(4) Output vertices of each DFT in DFF as a separate SCC

Cb={b,a,e}Cg={g,f} Ch={h}

Cc={c,d}

119/124 CR09

Page 120: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Example

CS473 – Lecture 16 Cevdet Aykanat - Bilkent University

Computer Engineering Department

20

SCC: Example

a b c

e

d

f g h

hf,g

c,d

a,b,e

Acyclic component

graph

Cb Cg

Cc

Ch

120/124 CR09

Page 121: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

Strongly connected components: Observations

In any DFS(G ), all vertices in the same SCC are placed in the sameDFT.

In the DFS(G ) step of the algorithm, the last vertex finished in anSCC is the first vertex discovered in the SCC.

Consider the vertex r with the largest finishing time. It is a root of aDFT. Any vertex that is reachable from r in GT should be in theSCC of r (why?)

121/124 CR09

Page 122: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Breadth-first searchDepth-first searchTopological sortStrongly connected components

SCC and reducibility

To detect if there exists a permutation matrix P such that

PAPT =

(A11 A12

O A22

),

where A11 is an r × r submatrix, A22 is an (n − r)× (n − r) submatrix,where 1 ≤ r < n:

run SCC on the directed graph of A to identify each strongly connectedcomponent as an irreducible block (more than one SCC?). Hence A11,too, can be in that form (how many SCCs?).

122/124 CR09

Page 123: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Could not get enough of it: Questions

How would you describe the following in the language of graphs

the structure of PAPT for a given square sparse matrix A and apermutation matrix P,

the structure of PAQ for a given square sparse matrix A and twopermutation matrices P and Q,

the structure of Ak , for k > 1,

the structure of AAT ,

the structure of the vector b, where b = Ax for a given sparsematrix A, and a sparse vector x .

123/124 CR09

Page 124: Introduction to graph theory and algorithmsgraal.ens-lyon.fr/~bucar/CR07/lecture-graphs.pdf · Outline Introduction to graph theory and algorithms Jean-Yves L’Excellent and Bora

Definitions and some problemsBasic algorithms

Questions

Could not get enough of it: Questions

Can you define:

the row-net hypergraph model of a matrix.

a matching in a hypergraph (is it a hard problem?).

Can you relate:

the DFS or BFS on a tree to a topological ordering? postordering?

Find an algorithm

how do you transpose a matrix in CSR or CSC format?

124/124 CR09