Top Banner
Applications of the depth-first search algorithm Undirected Graphs Lecture 3 Mircea Marin October 2013 Mircea Marin Applications of the depth-first search algorithm Undirected Graphs
43

Advanced Data Structures UVT

May 14, 2017

Download

Documents

Dan Heisenberg
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advanced Data Structures UVT

Applications of the depth-first search algorithmUndirected Graphs

Lecture 3

Mircea Marin

October 2013

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 2: Advanced Data Structures UVT

Outline

Applications of the depth-first search algorithmDepth-first spanning forestDAG recognitionTopological sort of DAGsComputation of strong components of digraphs

Undirected graphsDefinition, main notionsMain operations

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 3: Advanced Data Structures UVT

Depth-first search

Given a graph G = (V ,E) and a node s ∈ VFind all nodes reachable by a path from s, by searching

"deeper" in the graph whenever possible.Edges are explored out of the most recentlydiscovered vertex v that still has unexplorededges leaving it.When all of v ’s edges have been explored,the algorithm "backtracks" to its parent node,to explore other leaving edges.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 4: Advanced Data Structures UVT

Depth-first traversal (DFT)Iterative version (see also Lecture 2)

procedure dfs(v : vertex)first-visit-op(v) /* first visit of v */S:= a stack containing only node vwhile S is not empty

u:=S.peek()if u has unvisited neighbors

w:=the first unvisited neighbor of ufirst-visit-op(w) /* first visit of w */mark[w]:=visitedS.push(w)

elsew:=S.pop()last-visit-op(w) /* last visit of w */

NOTES:

I mark is a global array (or map) initialized withmark[w ] := unvisited for all nodes w

I a node is marked as visited when it is first encountered.Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 5: Advanced Data Structures UVT

Compare with breadth-first traversalA simplified version of breadth first-search (see also Lecture 2)

procedure bfs(v : vertex)first-visit-op(v) /* first visit of v */Q:= a queue containing only node vwhile Q is not empty

extract last element u from Qlast-visit-op(u) /* last visit of u */for all neighbours w of u do

if mark[w ] = unvisitedmark[w ] := visitedfirst-visit-op(w) /* first visit of w */Q.push(w)

NOTES:

I mark is a global array (or map) initialized withmark[w ] := unvisited for all nodes w

I a node is marked as visited when it is first encountered.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 6: Advanced Data Structures UVT

Depth-first traversal (DFT)Recursive version

DFT is more natural to describe recursively (pseudocode):

procedure dfs(v : vertex)mark[v] := visitedfirst-visit-op(v) /* first visit of v */for each neighbor w of v do

if mark[w]=unvisited thendfs(w) /* recursive call */

last-visit-op(v) /* last visit of v */

NOTES:

I mark is a global array (or map) initialized withmark[w ] := unvisited for all nodes w

I a node is marked as visited when it is first visited.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 7: Advanced Data Structures UVT

Applications of the depth-first search algorithmDepth-first spanning forest

Consider the digraph G E

F

G

B

D

A

C

with adjacency listsA 7→ [B,C] B 7→ [C,D] C 7→ [A]D 7→ [C] E 7→ [F ,G]F 7→ [B] G 7→ [F ,D]

Some depth-first traversals of G do not visit all nodes of G:A

BC D

E

F G

I Nodes E ,F ,G are not visited.

By selecting unvisited nodes as long as possible andrestarting depth first traversal from them, we produce adepth-first spanning forest.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 8: Advanced Data Structures UVT

Applications of the depth-first search algorithmDepth-first spanning forest

Consider the digraph G E

F

G

B

D

A

C

with adjacency listsA 7→ [B,C] B 7→ [C,D] C 7→ [A]D 7→ [C] E 7→ [F ,G]F 7→ [B] G 7→ [F ,D]

Some depth-first traversals of G do not visit all nodes of G:A

BC D

E

F G

I Nodes E ,F ,G are not visited.By selecting unvisited nodes as long as possible andrestarting depth first traversal from them, we produce adepth-first spanning forest.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 9: Advanced Data Structures UVT

Applications of the depth-first search algorithmDepth-first spanning forest

Consider the digraph G E

F

G

B

D

A

C

with adjacency listsA 7→ [B,C] B 7→ [C,D] C 7→ [A]D 7→ [C] E 7→ [F ,G]F 7→ [B] G 7→ [F ,D]

Some depth-first traversals of G do not visit all nodes of G:A

BC D

E

F G

I Nodes E ,F ,G are not visited.By selecting unvisited nodes as long as possible andrestarting depth first traversal from them, we produce adepth-first spanning forest.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 10: Advanced Data Structures UVT

Applications of the depth-first search algorithmDepth-first spanning forest

A1

B2

C 3 D4

E5

F6

G7

dfnumber [A] = 1dfnumber [B] = 2dfnumber [C] = 3dfnumber [D] = 4. . .

Drawing strategy:

The nodes of depth first-search tree are drawn from left to right, in the order oftheir first visit.

Assign a number dfnumber [n] to every node n, starting from 1, in the orderin which they are visited first by the depth-first search.

The trees are drawn from left to right, in the order in which they are generated.

Remarks: There are 4 kinds of arcs

1 The solid arcs connecting nodes in the depth-first search trees are tree arcs.2 The other arcs, which are dashed, are of 3 kinds:

(1) back arcs: connect a node with an ancestor in its tree: C → A, D → A(2) forward arcs: connect a node with a descendant in its tree: A→ C(3) cross arcs: all other arcs: F → B,G→ F ,G→ D

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 11: Advanced Data Structures UVT

Applications of the depth-first search algorithmDepth-first spanning forest

A1

B2

C 3 D4

E5

F6

G7

dfnumber [A] = 1dfnumber [B] = 2dfnumber [C] = 3dfnumber [D] = 4. . .

Drawing strategy:

The nodes of depth first-search tree are drawn from left to right, in the order oftheir first visit.

Assign a number dfnumber [n] to every node n, starting from 1, in the orderin which they are visited first by the depth-first search.

The trees are drawn from left to right, in the order in which they are generated.

Remarks: There are 4 kinds of arcs

1 The solid arcs connecting nodes in the depth-first search trees are tree arcs.2 The other arcs, which are dashed, are of 3 kinds:

(1) back arcs: connect a node with an ancestor in its tree: C → A, D → A(2) forward arcs: connect a node with a descendant in its tree: A→ C(3) cross arcs: all other arcs: F → B,G→ F ,G→ D

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 12: Advanced Data Structures UVT

Applications of the depth-first search algorithmDepth-first spanning forest

A1

B2

C 3 D4

E5

F6

G7

dfnumber [A] = 1dfnumber [B] = 2dfnumber [C] = 3dfnumber [D] = 4. . .

Drawing strategy:

The nodes of depth first-search tree are drawn from left to right, in the order oftheir first visit.

Assign a number dfnumber [n] to every node n, starting from 1, in the orderin which they are visited first by the depth-first search.

The trees are drawn from left to right, in the order in which they are generated.

Remarks: There are 4 kinds of arcs1 The solid arcs connecting nodes in the depth-first search trees are tree arcs.

2 The other arcs, which are dashed, are of 3 kinds:(1) back arcs: connect a node with an ancestor in its tree: C → A, D → A(2) forward arcs: connect a node with a descendant in its tree: A→ C(3) cross arcs: all other arcs: F → B,G→ F ,G→ D

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 13: Advanced Data Structures UVT

Applications of the depth-first search algorithmDepth-first spanning forest

A1

B2

C 3 D4

E5

F6

G7

dfnumber [A] = 1dfnumber [B] = 2dfnumber [C] = 3dfnumber [D] = 4. . .

Drawing strategy:

The nodes of depth first-search tree are drawn from left to right, in the order oftheir first visit.

Assign a number dfnumber [n] to every node n, starting from 1, in the orderin which they are visited first by the depth-first search.

The trees are drawn from left to right, in the order in which they are generated.

Remarks: There are 4 kinds of arcs1 The solid arcs connecting nodes in the depth-first search trees are tree arcs.2 The other arcs, which are dashed, are of 3 kinds:

(1) back arcs: connect a node with an ancestor in its tree: C → A, D → A(2) forward arcs: connect a node with a descendant in its tree: A→ C(3) cross arcs: all other arcs: F → B,G→ F ,G→ D

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 14: Advanced Data Structures UVT

Applications of the depth-first search algorithmDepth-first spanning forest

A1

B2

C3

D4

E5

F6

G7

dfnumber [A] = 1dfnumber [B] = 2dfnumber [C] = 3dfnumber [D] = 4. . .

Characterizations of the node relations by vertex numbering

1 A node u is a descendant of v iff dfnumber [v ] ≤ dfnumber [u] ≤ dfnumber [v ]+number of descendants of v . Thus:

forward arcs go from low-numbered nodes to high-numbered nodes.back arcs go from high-numbered nodes to low-numbered nodes.

2 All cross arcs go from high-numbered to low-numbered nodes.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 15: Advanced Data Structures UVT

Applications of the depth-first search algorithmDepth-first spanning forest

A1

B2

C3

D4

E5

F6

G7

dfnumber [A] = 1dfnumber [B] = 2dfnumber [C] = 3dfnumber [D] = 4. . .

Characterizations of the node relations by vertex numbering

1 A node u is a descendant of v iff dfnumber [v ] ≤ dfnumber [u] ≤ dfnumber [v ]+number of descendants of v . Thus:

forward arcs go from low-numbered nodes to high-numbered nodes.back arcs go from high-numbered nodes to low-numbered nodes.

2 All cross arcs go from high-numbered to low-numbered nodes.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 16: Advanced Data Structures UVT

Applications of the depth-first search algorithmDepth-first spanning forest

A1

B2

C3

D4

E5

F6

G7

dfnumber [A] = 1dfnumber [B] = 2dfnumber [C] = 3dfnumber [D] = 4. . .

Characterizations of the node relations by vertex numbering1 A node u is a descendant of v iff dfnumber [v ] ≤ dfnumber [u] ≤ dfnumber [v ]+

number of descendants of v . Thus:

forward arcs go from low-numbered nodes to high-numbered nodes.back arcs go from high-numbered nodes to low-numbered nodes.

2 All cross arcs go from high-numbered to low-numbered nodes.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 17: Advanced Data Structures UVT

Applications of the depth-first search algorithmDepth-first spanning forest

A1

B2

C3

D4

E5

F6

G7

dfnumber [A] = 1dfnumber [B] = 2dfnumber [C] = 3dfnumber [D] = 4. . .

Characterizations of the node relations by vertex numbering1 A node u is a descendant of v iff dfnumber [v ] ≤ dfnumber [u] ≤ dfnumber [v ]+

number of descendants of v . Thus:

forward arcs go from low-numbered nodes to high-numbered nodes.back arcs go from high-numbered nodes to low-numbered nodes.

2 All cross arcs go from high-numbered to low-numbered nodes.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 18: Advanced Data Structures UVT

Directed acyclic graphs (DAGs)A directed acyclic graph (DAG for short) is a digraph without cycles.

1 DAGs are useful in representing the syntactic structure of arithmetic expressionswith common subexpressions.

Example (DAG for (a + b) ∗ c + ((a + b) + e) ∗ (e + f )) ∗ (a + b) ∗ c)

a b+

∗c

+

e f

∗+

∗+

2 DAGs can be used to represent partial orders (=irreflexive and transitive).

Example (DAG for the relation ⊂ between subsets of {1, 2, 3})

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 19: Advanced Data Structures UVT

Directed acyclic graphs (DAGs)A directed acyclic graph (DAG for short) is a digraph without cycles.

1 DAGs are useful in representing the syntactic structure of arithmetic expressionswith common subexpressions.

Example (DAG for (a + b) ∗ c + ((a + b) + e) ∗ (e + f )) ∗ (a + b) ∗ c)

a b+

∗c

+

e f

∗+

∗+

2 DAGs can be used to represent partial orders (=irreflexive and transitive).

Example (DAG for the relation ⊂ between subsets of {1, 2, 3})

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 20: Advanced Data Structures UVT

Directed acyclic graphs (DAGs)A directed acyclic graph (DAG for short) is a digraph without cycles.

1 DAGs are useful in representing the syntactic structure of arithmetic expressionswith common subexpressions.

Example (DAG for (a + b) ∗ c + ((a + b) + e) ∗ (e + f )) ∗ (a + b) ∗ c)

a b+

∗c

+

e f

∗+

∗+

2 DAGs can be used to represent partial orders (=irreflexive and transitive).

Example (DAG for the relation ⊂ between subsets of {1, 2, 3})

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 21: Advanced Data Structures UVT

Applications of depth-first search

1 Test for acyclicity: Determine whether a digraph is acyclic.Solution: Generate a depth-first spanning forest.

The digraph is acyclic iff it contains a back arc.2 Topological sort

Scenario

A large project is often divided into a collection of smaller tasks, some of which have tobe performed in certain specified orders so that we may complete the entire project.For example, a university curriculum may have courses that require other courses asprerequisites. We can model the required orders with a DAG, e.g.:

C1

C2

C3

C4

C5

Requirement: Assign a linear ordering to the nodes of a DAG s.t. if there is an arcfrom i to j then i occurs before j in the linear ordering.The linear ordering is called a topological sort.

Topological sort can be accomplished easily with depth first search.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 22: Advanced Data Structures UVT

Applications of depth-first search

1 Test for acyclicity: Determine whether a digraph is acyclic.Solution: Generate a depth-first spanning forest.

The digraph is acyclic iff it contains a back arc.

2 Topological sort

Scenario

A large project is often divided into a collection of smaller tasks, some of which have tobe performed in certain specified orders so that we may complete the entire project.For example, a university curriculum may have courses that require other courses asprerequisites. We can model the required orders with a DAG, e.g.:

C1

C2

C3

C4

C5

Requirement: Assign a linear ordering to the nodes of a DAG s.t. if there is an arcfrom i to j then i occurs before j in the linear ordering.The linear ordering is called a topological sort.

Topological sort can be accomplished easily with depth first search.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 23: Advanced Data Structures UVT

Applications of depth-first search

1 Test for acyclicity: Determine whether a digraph is acyclic.Solution: Generate a depth-first spanning forest.

The digraph is acyclic iff it contains a back arc.2 Topological sort

Scenario

A large project is often divided into a collection of smaller tasks, some of which have tobe performed in certain specified orders so that we may complete the entire project.For example, a university curriculum may have courses that require other courses asprerequisites. We can model the required orders with a DAG, e.g.:

C1

C2

C3

C4

C5

Requirement: Assign a linear ordering to the nodes of a DAG s.t. if there is an arcfrom i to j then i occurs before j in the linear ordering.The linear ordering is called a topological sort.

Topological sort can be accomplished easily with depth first search.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 24: Advanced Data Structures UVT

Applications of depth-first search

1 Test for acyclicity: Determine whether a digraph is acyclic.Solution: Generate a depth-first spanning forest.

The digraph is acyclic iff it contains a back arc.2 Topological sort

Scenario

A large project is often divided into a collection of smaller tasks, some of which have tobe performed in certain specified orders so that we may complete the entire project.For example, a university curriculum may have courses that require other courses asprerequisites. We can model the required orders with a DAG, e.g.:

C1

C2

C3

C4

C5

Requirement: Assign a linear ordering to the nodes of a DAG s.t. if there is an arcfrom i to j then i occurs before j in the linear ordering.The linear ordering is called a topological sort.

Topological sort can be accomplished easily with depth first search.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 25: Advanced Data Structures UVT

Applications of Depth-First SearchTopological sort using depth-first search

topsort(node v) is the depth-first search procedure adjusted with a statementto print the nodes accessible from a node v in reverse topological order.

Pseudocode

void topsort(node v) {nodeColor[v]=BLACK; // mark v as visitedfor every node w adjacent to v do

if (nodeColor[w] == WHITE) { // if w is not visitedtopsort(w);

}cout << v.getName();

}

I Remark. When topsort finishes searching all nodes adjacent to a node x , itprints x .

⇒ topsort(v) will print in a reverse topological order all nodes of a DAG whichare accessible from v by a path in the DAG.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 26: Advanced Data Structures UVT

Applications of Depth-First SearchTopological sort using depth-first search

topsort(node v) is the depth-first search procedure adjusted with a statementto print the nodes accessible from a node v in reverse topological order.

Pseudocode

void topsort(node v) {nodeColor[v]=BLACK; // mark v as visitedfor every node w adjacent to v do

if (nodeColor[w] == WHITE) { // if w is not visitedtopsort(w);

}cout << v.getName();

}

I Remark. When topsort finishes searching all nodes adjacent to a node x , itprints x .

⇒ topsort(v) will print in a reverse topological order all nodes of a DAG whichare accessible from v by a path in the DAG.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 27: Advanced Data Structures UVT

Applications of Depth-First SearchTopological sort using depth-first search

topsort(node v) is the depth-first search procedure adjusted with a statementto print the nodes accessible from a node v in reverse topological order.

Pseudocode

void topsort(node v) {nodeColor[v]=BLACK; // mark v as visitedfor every node w adjacent to v do

if (nodeColor[w] == WHITE) { // if w is not visitedtopsort(w);

}cout << v.getName();

}

I Remark. When topsort finishes searching all nodes adjacent to a node x , itprints x .

⇒ topsort(v) will print in a reverse topological order all nodes of a DAG whichare accessible from v by a path in the DAG.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 28: Advanced Data Structures UVT

Applications of Depth-First SearchTopological sort using depth-first search

topsort(node v) is the depth-first search procedure adjusted with a statementto print the nodes accessible from a node v in reverse topological order.

Pseudocode

void topsort(node v) {nodeColor[v]=BLACK; // mark v as visitedfor every node w adjacent to v do

if (nodeColor[w] == WHITE) { // if w is not visitedtopsort(w);

}cout << v.getName();

}

I Remark. When topsort finishes searching all nodes adjacent to a node x , itprints x .

⇒ topsort(v) will print in a reverse topological order all nodes of a DAG whichare accessible from v by a path in the DAG.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 29: Advanced Data Structures UVT

Applications of Depth-First SearchStrong components

Assumption: G = (V ,E) is a digraph.A strongly connected component of G is a subgraph (V ′,E ′) ofG where V ′ is a maximal subset of V s.t. there is a path fromany node in the set to any other node in the set.

v ≡ w iff there exists a path from v to w and also a pathfrom w to v is an equivalence relation on V

⇒ V can be partitioned into a family {Vi | 1 ≤ i ≤ r} ofequivalence classes w.r.t. ≡, and then build the strongcomponents Gi = (Vi ,Ei) of G.

A digraph with only one strong component is calledstrongly connected.

Example

a b

cdhas 2 strong components:

a b

cd

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 30: Advanced Data Structures UVT

Applications of Depth-First SearchStrong components

Assumption: G = (V ,E) is a digraph.A strongly connected component of G is a subgraph (V ′,E ′) ofG where V ′ is a maximal subset of V s.t. there is a path fromany node in the set to any other node in the set.

v ≡ w iff there exists a path from v to w and also a pathfrom w to v is an equivalence relation on V

⇒ V can be partitioned into a family {Vi | 1 ≤ i ≤ r} ofequivalence classes w.r.t. ≡, and then build the strongcomponents Gi = (Vi ,Ei) of G.

A digraph with only one strong component is calledstrongly connected.

Example

a b

cdhas 2 strong components:

a b

cd

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 31: Advanced Data Structures UVT

Applications of Depth-First SearchStrong components

Remarks

Every node of a digraph belongs to a strong component.

Some arcs of a digraph may not belong to a strong component. Such arcs arecross-component arcs.

The strong components of a digraph G can be found by constructing a reducedgraph G′ of G:

I the nodes of G′ are the strong components of GI there is an arc from C in C′ in G′ iff there is an arc in G

from some node in C to some node in C′.

Example:

a b

cdhas the reduced graph

a,b, c

d

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 32: Advanced Data Structures UVT

Applications of Depth-First SearchComputation of strong components

1 Perform a depth-first search of G and number the nodes inpost-order traversal of the depth-first search tree (that is,assign to the parent node a larger number than to itschildren)

2 Construct the digraph Gr obtained from G by reversing thedirection of all its arcs.

3 Perform depth-first search of Gr , starting the search fromthe node with highest number assigned in step 1. Ifdepth-first search does not reach all nodes, start the nextdepth-first search from the highest-numbered remainingnode.

4 Each tree in the resulting spanning forest is a stronglyconnected component of G.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 33: Advanced Data Structures UVT

Computation of strong componentsExample

a b

cdFig. 1

a4 b 3

c1 d 2

Fig. 2

da

c

b3

2

4 1

Fig. 3

I Start performing depth-first search from node a in Fig. 1;number increasingly the vertices during the time of theirlast visit (step 1) and revert the arcs of G (step 2)⇒digraph Gr shown in Fig. 2.

I Perform depth-first search in Gr starting with a because ahas highest assigned number. The first depth-first searchtree will contain nodes a,b, c. Then, continue with root d tobuild the next depth-first search tree . . .

⇒ Depth-first spanning forest shown in Fig. 3.Each tree of the forest forms a strong component of theoriginal graph.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 34: Advanced Data Structures UVT

Computation of strong componentsExample

a b

cdFig. 1

a4 b 3

c1 d 2

Fig. 2

da

c

b3

2

4 1

Fig. 3

I Start performing depth-first search from node a in Fig. 1;number increasingly the vertices during the time of theirlast visit (step 1) and revert the arcs of G (step 2)⇒digraph Gr shown in Fig. 2.

I Perform depth-first search in Gr starting with a because ahas highest assigned number. The first depth-first searchtree will contain nodes a,b, c. Then, continue with root d tobuild the next depth-first search tree . . .

⇒ Depth-first spanning forest shown in Fig. 3.Each tree of the forest forms a strong component of theoriginal graph.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 35: Advanced Data Structures UVT

Computation of strong componentsExample

a b

cdFig. 1

a4 b 3

c1 d 2

Fig. 2

da

c

b3

2

4 1

Fig. 3

I Start performing depth-first search from node a in Fig. 1;number increasingly the vertices during the time of theirlast visit (step 1) and revert the arcs of G (step 2)⇒digraph Gr shown in Fig. 2.

I Perform depth-first search in Gr starting with a because ahas highest assigned number. The first depth-first searchtree will contain nodes a,b, c. Then, continue with root d tobuild the next depth-first search tree . . .

⇒ Depth-first spanning forest shown in Fig. 3.Each tree of the forest forms a strong component of theoriginal graph.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 36: Advanced Data Structures UVT

Computation of strong componentsExample

a b

cdFig. 1

a4 b 3

c1 d 2

Fig. 2

da

c

b3

2

4 1

Fig. 3

I Start performing depth-first search from node a in Fig. 1;number increasingly the vertices during the time of theirlast visit (step 1) and revert the arcs of G (step 2)⇒digraph Gr shown in Fig. 2.

I Perform depth-first search in Gr starting with a because ahas highest assigned number. The first depth-first searchtree will contain nodes a,b, c. Then, continue with root d tobuild the next depth-first search tree . . .⇒ Depth-first spanning forest shown in Fig. 3.

Each tree of the forest forms a strong component of theoriginal graph.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 37: Advanced Data Structures UVT

Computation of strong componentsExample

a b

cdFig. 1

a4 b 3

c1 d 2

Fig. 2

da

c

b3

2

4 1

Fig. 3

I Start performing depth-first search from node a in Fig. 1;number increasingly the vertices during the time of theirlast visit (step 1) and revert the arcs of G (step 2)⇒digraph Gr shown in Fig. 2.

I Perform depth-first search in Gr starting with a because ahas highest assigned number. The first depth-first searchtree will contain nodes a,b, c. Then, continue with root d tobuild the next depth-first search tree . . .⇒ Depth-first spanning forest shown in Fig. 3.

Each tree of the forest forms a strong component of theoriginal graph.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 38: Advanced Data Structures UVT

Undirected graphsG = (V ,E) where

V = finite set of vertices (or nodes)

E = finite set of edges between nodes.Every edge e ∈ E connects two nodes u, v ∈ V called the nodes incident to e.

An undirected graph can be

simple: there is at most one edge between any two nodes. An edge between

nodes u, v is represented as u−v and drawn asu v .

It is assumed that edges are unordered, thus u−v = v−u.If u−v is an edge than we say nodes u and v are adjacent.

multiple: there can be more than one edge between two nodes.Distinct edges between same nodes are distinguished by labelingthem. An edge with label e between nodes u and v is represented as

ue− v and drawn as

u ve

.

It is assumed that edges are unordered, thus ue− v = v

e− u.

path = sequence of nodes π = v1, . . . , vn such that v1−v2, . . . , vn−1−vn ∈ E .The length of π is n − 1. π is a path between v1 and vn.

simple path = path with distinct nodes, except possibly the first and last.

simple cycle = path of length at least 1 that begins ad ends at the same node.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 39: Advanced Data Structures UVT

Examples of undirected graphs

Acquaintanceship graph=simple graph where edges connectpeople that know each other.

Computer network: multigraph where edges are between citieslinked by computer connections.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 40: Advanced Data Structures UVT

Auxiliary notions

Assumption: G = (V ,E) undirected graph.Subgraph of G: G′ = (V ′,E ′) where V ′ ⊆ V and E ′ = the set of edges in E

between nodes in V ′.

a b

cd

V = {a,b, c,d}

a b

c

V ′ = {a,b, c}

Two nodes are connected if there is a path between them.

A connected component of G is a subgraph G′ = (V ′,E ′) with V ′ maximal suchthat every u, v ∈ V ′ are connected.

Figure : An unconnected graph with 2 connected components

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 41: Advanced Data Structures UVT

Undirected graphs: Methods of RepresentationAdjacency matrix, adjacency lists, and edge list

a b

cd

1 Adjacency matrixNote: This is a symmetric matrix.

2 Adjacency lists

3 Edge list: [a−b,b−c, c−d ,d−a,d−b]

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 42: Advanced Data Structures UVT

Operations on Undirected Graphs

I All operations on directed graphs carry over to undirectedgraphs, with the difference that every undirected edge u−v maybe thought as the pair of directed edges u → v and v → u.

I Terminology specific to unconnected graphs G = (V ,E)

Connected component = maximal connected inducedsubgraph G′ = (V ′,E ′) of G, that is:

B All nodes of V ′ are connectedB There is no path between a node in V ′ and a node in V − V ′.

Free tree = a connected acyclic graph.

Properties of depth-first spanning forests in undirected graphs:1 There is no distinction between forward and backward

edges.2 There are no cross edges, that is, edges v−w such that v

is neither ancestor nor descendant of w .

⇒ all edges are either tree edges or back edges.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs

Page 43: Advanced Data Structures UVT

References

I Alfred V. Aho et al. Data Structures and Algorithms.Chapters 6 and 7.

I K. H. Rosen. Discrete Mathematics and Its Applications.Chapter 9.

Mircea Marin Applications of the depth-first search algorithm Undirected Graphs