0% found this document useful (0 votes)
71 views111 pages

Unit #6 Exploring Graphs: Prof - Riddhi Kotak

The document describes different types of graphs and their representations. It defines graphs as consisting of vertices and edges, and describes undirected and directed graphs. It also defines weighted, dense, sparse, connected, biconnected, articulation points, and bridges in graphs. The document discusses representing graphs using adjacency matrices and adjacency lists and compares their storage requirements and time complexities.

Uploaded by

Piyush Davda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views111 pages

Unit #6 Exploring Graphs: Prof - Riddhi Kotak

The document describes different types of graphs and their representations. It defines graphs as consisting of vertices and edges, and describes undirected and directed graphs. It also defines weighted, dense, sparse, connected, biconnected, articulation points, and bridges in graphs. The document discusses representing graphs using adjacency matrices and adjacency lists and compares their storage requirements and time complexities.

Uploaded by

Piyush Davda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 111

UNIT #6

EXPLORING GRAPHS
Prof.Riddhi kotak
Introduction
2
 Graph G = (V, E)
 V = set of vertices
 E = set of edges  (V  V)

A A
B B

C C
D D
E E

Undirected Graph Directed Graph


Prof.Riddhi kotak Thursday, July 25, 2019
Introduction
3

 Types of graphs
 Undirected: edge (u, v) = (v, u);
for all v, (v, v)  E (No self loops.)
 Directed: (u, v) is edge from u to v,
denoted as u  v. Self loops are allowed.
 Weighted: each edge has an associated
weight, given by a weight function w : E 
R.
 Dense: |E|  |V|2.
 Sparse: |E| << |V|2.
Prof.Riddhi kotak Thursday, July 25, 2019
Introduction
4

 If (u, v)  E, then vertex v is adjacent to


vertex u.
 Adjacency relationship is:
 Symmetric if G is undirected.
 Not necessarily so if G is directed.
 If G is connected:
 There is a path between every pair of vertices.
 |E|  |V| – 1.
 Furthermore, if |E| = |V| – 1, then G is a tree.

Prof.Riddhi kotak Thursday, July 25, 2019


Connectivity
5

 An undirected graph is said to be


connected
 if for any pair of nodes of the graph, the
two nodes, are reachable from one another
(i.e. there is a path between them).

 If starting from any vertex we can visit


all other vertices, then the graph is
connected

Prof.Riddhi kotak Thursday, July 25, 2019


Connectivity
6
 A graph is biconnected,
 if there are no vertices whose removal will
disconnect the graph.

A A

B B

C C
D D

E E

Biconnected Not Biconnected


Prof.Riddhi kotak Thursday, July 25, 2019
Connectivity
7
 Articulation Points
 A vertex whose removal makes the graph
disconnected is called an articulation
point or cut-vertex.
We can compute
A
articulation points using
B
depth-first search and a
C
special numbering of the
D vertices in the order of their
E visiting.

C is an articulation point
Prof.Riddhi kotak Thursday, July 25, 2019
Connectivity
8
 Bridges
 An edge in a graph is called a bridge, if its
removal disconnects the graph.

Any edge in a graph, that


A
does not lie on a cycle, is a
B bridge.
Obviously, a bridge has at
C
D least one articulation point at
its end, however an
E
articulation point is not
(C,D) and (E,D) are bridges necessarily linked in a bridge.
Prof.Riddhi kotak Thursday, July 25, 2019
Connectivity
9

A B

C D

E F

 C is an articulation point, there are no bridges

Prof.Riddhi kotak Thursday, July 25, 2019


Connectivity
10

A B

C D

E F

 C is an articulation point, CB is a bridge

Prof.Riddhi kotak Thursday, July 25, 2019


Connectivity
11
A

 Biconnected graph:
no articulation points and no bridges

Prof.Riddhi kotak Thursday, July 25, 2019


Connectivity
12
 A directed graph is said to be strongly
connected
 if for any pair of nodes there is a path from
each one to the other.

A
B

C
D
E

Prof.Riddhi kotak Thursday, July 25, 2019


Connectivity
13
 A directed graph is said to be
unilaterally connected
 if for any pair of nodes at least one of the
nodes is reachable from the other

A
Each strongly connected
B
graph is also unilaterally
C connected.
D
E

Prof.Riddhi kotak Thursday, July 25, 2019


Connectivity
14
 A directed graph is said to be weakly
connected
 if the underlying undirected graph is
connected

There is no path between


A
B and D
B
Each unilaterally
C
D connected graph is also
weakly connected
E

Prof.Riddhi kotak Thursday, July 25, 2019


Representation of Graphs
15
 Two standard ways.
A B

C D
Adjacency Matrix Adjacency Lists

A B C D A B C D /
A 0 1 1 1
B A C /
B 1 0 1 0
C 1 1 0 1 C A B D /
D 1 0 1 0
D A C /

Prof.Riddhi kotak Thursday, July 25, 2019


Adjacency Matrix
16
 |V|  |V| matrix A.
 Number vertices from 1 to |V| in some
arbitrary manner.
 A is then given by:A[i, j ]  a  1 if (i, j )  E
ij 
0 otherwise
1 2 3 4 1 2 3 4
1 2 1 2
1 0 1 1 1 1 0 1 1 1
2 1 0 1 0 2 0 0 1 0
3 1 1 0 1 3 0 0 0 1
3 4 3 4
4 1 0 1 0 4 0 0 0 0
 A = AT for undirected graphs.
Prof.Riddhi kotak Thursday, July 25, 2019
Adjacency Matrix
17

 Space: (V2).
 Not memory efficient for large graphs.
 Time: to list all vertices adjacent to u:
(V).
 Time: to determine if (u, v)  E: (1).
 Can store weights instead of bits for
weighted graph.

Prof.Riddhi kotak Thursday, July 25, 2019


Adjacency Lists
18

 Consists of an array Adj of |V| lists.


 One list per vertex.
 For u  V, Adj[u] consists of all vertices
adjacent to u.

 If weighted, store weights also in


adjacency lists.

Prof.Riddhi kotak Thursday, July 25, 2019


Adjacency Lists
19

A B C D /
A B
B A C /

C A B D /
C D
D A C /

A B C D /
A B
B C /

C D /
C D
D /

Prof.Riddhi kotak Thursday, July 25, 2019


Adjacency Lists
20

 Storage Requirement
 For directed graphs:
 Sum of lengths of all adj. lists is
out-degree(v) = |E|
vV

 Total storage: (V+E)


 For undirected graphs:
 Sum of lengths of all adj. lists is
degree(v) = 2|E|
vV

 Total storage: (V+E)

Prof.Riddhi kotak Thursday, July 25, 2019


Traversing/Searching
21
Graphs
 Systematically follow the edges of a graph
to visit the vertices of the graph.
 Used to discover the structure of a graph.
 Standard tree-traversal algorithms.
 Pre-order [root-left-right]
 In-order [left-root-right]
 Post-order [left-right-root]
 Standard graph-searching algorithms.
 Breadth-first Search (BFS).
 Depth-first Search (DFS).

Prof.Riddhi kotak Thursday, July 25, 2019


Breadth-first Search
22

 Input:
 Graph G = (V, E), either directed or
undirected, and source vertex s  V.
 Output:
 d[v] = distance (smallest # of edges, or
shortest path) from s to v, for all v  V. d[v]
=  if v is not reachable from s.
 [v] = u such that (u, v) is last edge on
shortest path s to v. [u is v’s predecessor.]
 Builds breadth-first tree with root s that
contains all reachable vertices.
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
23

 Expands the frontier between discovered


and undiscovered vertices uniformly across
the breadth of the frontier.
 A vertex is “discovered” the first time it is
encountered during the search.
 A vertex is “finished” if all vertices adjacent to it
have been discovered.
 Colors the vertices to keep track of progress.
 White – Undiscovered.
 Gray – Discovered but not finished.
 Black – Finished.

Prof.Riddhi kotak Thursday, July 25, 2019


Breadth-first Search
24
BFS(G, s)
1. for each vertex u in V[G] – {s} do
2. color[u]  white
3. d[u]  
4. [u]  nil
5. color[s]  gray
6. d[s]  0 white: undiscovered
7. [s]  nil gray: discovered
black: finished
8. Q  
9. enqueue(Q, s) Q: a queue of discovered vertices
color[v]: color of v
d[v]: distance from s to v
[u]: predecessor of v
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
25
10. while Q   do
11. u  dequeue(Q)
12. for each v in Adj[u] do
13. if color[v] = white
14. then color[v]  gray
15. d[v]  d[u] + 1
16. [v]  u white: undiscovered
17. enqueue(Q, v) gray: discovered
black: finished
18. color[u]  black
Q: a queue of discovered vertices
color[v]: color of v
d[v]: distance from s to v
[u]: predecessor of v
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
26

V[G] d[v] [v]

r s t u r
s
t
u
v w x y
v
w
x
y
Q:

Prof.Riddhi kotak Thursday, July 25, 2019


Breadth-first Search
27

r s t u V[G] d[v] [v]

 0   r  -
s 0 -
t  -
u  -
   
v  -
v w x y w  -
x  -
y  -
Q:s

Prof.Riddhi kotak Thursday, July 25, 2019


Breadth-first Search
28

r s t u V[G] d[v] [v]

 0   r  -
s 0 -
t  -
u  -
   
v  -
v w x y w  -
x  -
y  -
Q:
u:s
v : r, w
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
29

r s t u V[G] d[v] [v]

1 0   r 1 s
s 0 -
t  -
u  -
 1  
v  -
v w x y w 1 s
x  -
y  -
Q : r, w
u:
v:
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
30

r s t u V[G] d[v] [v]

1 0   r 1 s
s 0 -
t  -
u  -
 1  
v  -
v w x y w 1 s
x  -
y  -
Q:w
u:r
v : s, v
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
31

r s t u V[G] d[v] [v]

1 0   r 1 s
s 0 -
t  -
u  -
2 1  
v 2 r
v w x y w 1 s
x  -
y  -
Q : w, v
u:
v:
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
32

r s t u V[G] d[v] [v]

1 0   r 1 s
s 0 -
t  -
u  -
2 1  
v 2 r
v w x y w 1 s
x  -
y  -
Q:v
u:w
v : s, t, x
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
33

r s t u V[G] d[v] [v]

1 0 2  r 1 s
s 0 -
t 2 w
u  -
2 1 2 
v 2 r
v w x y w 1 s
x 2 w
y  -
Q : v, t, x
u:
v:
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
34

r s t u V[G] d[v] [v]

1 0 2  r 1 s
s 0 -
t 2 w
u  -
2 1 2 
v 2 r
v w x y w 1 s
x 2 w
y  -
Q : t, x
u:v
v:r
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
35

r s t u V[G] d[v] [v]

1 0 2  r 1 s
s 0 -
t 2 w
u  -
2 1 2 
v 2 r
v w x y w 1 s
x 2 w
y  -
Q : t, x
u:
v:
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
36

r s t u V[G] d[v] [v]

1 0 2  r 1 s
s 0 -
t 2 w
u  -
2 1 2 
v 2 r
v w x y w 1 s
x 2 w
y  -
Q:x
u:t
v : u, w, x
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
37

r s t u V[G] d[v] [v]

1 0 2 3 r 1 s
s 0 -
t 2 w
u 3 t
2 1 2 
v 2 r
v w x y w 1 s
x 2 w
y  -
Q : x, u
u:
v:
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
38

r s t u V[G] d[v] [v]

1 0 2 3 r 1 s
s 0 -
t 2 w
u 3 t
2 1 2 
v 2 r
v w x y w 1 s
x 2 w
y  -
Q:u
u:x
v : t, w, y
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
39

r s t u V[G] d[v] [v]

1 0 2 3 r 1 s
s 0 -
t 2 w
u 3 t
2 1 2 3
v 2 r
v w x y w 1 s
x 2 w
y 3 x
Q : u, y
u:
v:
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
40

r s t u V[G] d[v] [v]

1 0 2 3 r 1 s
s 0 -
t 2 w
u 3 t
2 1 2 3
v 2 r
v w x y w 1 s
x 2 w
y 3 x
Q:y
u:u
v : t, y
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
41

r s t u V[G] d[v] [v]

1 0 2 3 r 1 s
s 0 -
t 2 w
u 3 t
2 1 2 3
v 2 r
v w x y w 1 s
x 2 w
y 3 x
Q:y
u:
v:
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
42

r s t u V[G] d[v] [v]

1 0 2 3 r 1 s
s 0 -
t 2 w
u 3 t
2 1 2 3
v 2 r
v w x y w 1 s
x 2 w
y 3 x
Q:
u:y
v : u, x
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
43

r s t u V[G] d[v] [v]

1 0 2 3 r 1 s
s 0 -
t 2 w
u 3 t
2 1 2 3
v 2 r
v w x y w 1 s
x 2 w
y 3 x
Q:
u:
v:
Prof.Riddhi kotak Thursday, July 25, 2019
Breadth-first Search
44

r s t u

1 0 2 3

2 1 2 3

v w x y

Breadth First Tree

Prof.Riddhi kotak Thursday, July 25, 2019


Breadth-first Search Time
45
Analysis
BFS(G, s)
1. for each vertex u in V[G] – {s} do
2. color[u]  white
3. d[u]  
4. [u]  nil
5. color[s]  gray O(V)
6. d[s]  0
7. [s]  nil O(V+E)
8. Q  
9. enqueue(Q, s)
10. while Q   do
11. u  dequeue(Q) O(V)
12. for each v in Adj[u] do
13. if color[v] = white (E)
14. then color[v]  gray
15. d[v]  d[u] + 1
16. [v]  u
17. enqueue(Q, v)
18. color[u]  black

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
46

 Explore edges out of the most recently discovered


vertex v.
 When all edges of v have been explored, backtrack
to explore other edges leaving the vertex from
which v was discovered (its predecessor).
 “Search as deep as possible first.”
 Continue until all vertices reachable from the
original source are discovered.
 If any undiscovered vertices remain, then one of
them is chosen as a new source and search is
repeated from that source.

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
47

 Input:
 G = (V, E), directed or undirected. No source vertex
given!
 Output:
 2 timestamps on each vertex. Integers between 1
and 2|V|.
 d[v] = discovery time (v turns from white to gray)
 f [v] = finishing time (v turns from gray to black)

 [v] : predecessor of v = u, such that v was discovered


during the scan of u’s adjacency list.
 Uses the same colouring scheme for vertices as
BFS.
Prof.Riddhi kotak Thursday, July 25, 2019
Depth-first Search
48
DFS(G)
1. for each vertex u  V[G]
2. do color[u]  white
3. [u]  NIL
4. time  0
5. for each vertex u  V[G] white: undiscovered
6. do if color[u] = white gray: discovered
black: finished
7. then DFS-Visit(u)
color[v]: color of v
[u]: predecessor of v
d[v]: discovery time of v
f[v]: finishing time of v
Uses a global timestamp time.
Prof.Riddhi kotak Thursday, July 25, 2019
Depth-first Search
49
DFS-Visit(u)
1. color[u]  GRAY
2. time  time + 1
3. d[u]  time
4. for each v  Adj[u]
5. do if color[v] = WHITE white: undiscovered
6. then [v]  u gray: discovered
black: finished
7. DFS-Visit(v)
8. color[u]  BLACK color[v]: color of v
[u]: predecessor of v
9. f[u]  time  time + 1 d[v]: discovery time of v
f[v]: finishing time of v
Uses a global timestamp time.
Prof.Riddhi kotak Thursday, July 25, 2019
Depth-first Search
50

V[G] [v] d[v] f[v]

u v w u
v
w
x
x y z
y
z

time:

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
51

u v w V[G] [v] d[v] f[v]


u -
v -
w -
x -
y -
x y z z -

time: 0

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
52

u v w V[G] [v] d[v] f[v]

1/ u - 1
v -
w -
x -
y -
x y z z -

time: 1

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
53

u v w V[G] [v] d[v] f[v]

1/ 2/ u - 1
v u 2
w -
x -
y -
x y z z -

time: 2

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
54

u v w V[G] [v] d[v] f[v]

1/ 2/ u - 1
v u 2
w -
x -
3/
y v 3
x y z z -

time: 3

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
55

u v w V[G] [v] d[v] f[v]

1/ 2/ u - 1
v u 2
w -
x y 4
4/ 3/
y v 3
x y z z -

time: 4

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
56

u v w V[G] [v] d[v] f[v]

1/ 2/ u - 1
v u 2
w -
x y 4 5
4/5 3/
y v 3
x y z z -

time: 5

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
57

u v w V[G] [v] d[v] f[v]

1/ 2/ u - 1
v u 2
w -
x y 4 5
4/5 3/6
y v 3 6
x y z z -

time: 6

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
58

u v w V[G] [v] d[v] f[v]

1/ 2/7 u - 1
v u 2 7
w -
x y 4 5
4/5 3/6
y v 3 6
x y z z -

time: 7

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
59

u v w V[G] [v] d[v] f[v]

1/8 2/7 u - 1 8
v u 2 7
w -
x y 4 5
4/5 3/6
y v 3 6
x y z z -

time: 8

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
60

u v w V[G] [v] d[v] f[v]

1/8 2/7 9/ u - 1 8
v u 2 7
w - 9
x y 4 5
4/5 3/6
y v 3 6
x y z z -

time: 9

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
61

u v w V[G] [v] d[v] f[v]

1/8 2/7 9/ u - 1 8
v u 2 7
w - 9
x y 4 5
4/5 3/6 10/
y v 3 6
x y z z w 10

time: 10

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
62

u v w V[G] [v] d[v] f[v]

1/8 2/7 9/ u - 1 8
v u 2 7
w - 9
10/ x y 4 5
4/5 3/6
11 y v 3 6
x y z z w 10 11

time: 11

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
63

u v w V[G] [v] d[v] f[v]

1/8 2/7
9/ u - 1 8
12
v u 2 7
w - 9 12
10/ x y 4 5
4/5 3/6
11 y v 3 6
x y z z w 10 11

time: 12

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search
64

u v w V[G] [v] d[v] f[v]

1/8 2/7
9/ u - 1 8
12
v u 2 7
w - 9 12
10/ x y 4 5
4/5 3/6
11 y v 3 6
x y z z w 10 11

Depth-first Forest

Prof.Riddhi kotak Thursday, July 25, 2019


Depth-first Search Time
65
Analysis
DFS(G) DFS-Visit(u)
1. for each vertex u  V[G] 1. color[u]  GRAY
2. do color[u]  white 2. time  time + 1 (E)
3. [u]  NIL (v) 3. d[u]  time
4. time  0 4. for each v  Adj[u]
5. for each vertex u  V[G] 5. do if color[v] = WHITE
6. do if color[u] = white 6. then [v]  u
7. then DFS-Visit(u) 7. DFS-Visit(v)
8. color[u]  BLACK
9. f[u]  time  time + 1

O(V+E)

Prof.Riddhi kotak Thursday, July 25, 2019


Example
66

r s t u

v w x y

Prof.Riddhi kotak Thursday, July 25, 2019


Classification of Edges in
67
DFS
 Tree edge: in the depth-first forest.
Found by exploring (u, v).
 If not Tree edge then:
 Back edge: (u, v), where u is a
descendant of v in the depth-first tree.
 Forward edge: (u, v), where v is a
descendant of u, but not a tree edge.
 Cross edge: any other edge. Can go
between vertices in same depth-first tree
or in different depth-first trees.
Prof.Riddhi kotak Thursday, July 25, 2019
Classification of Edges in
68
DFS
u v w
T 9/
1/8 2/7
12

F B T C T

10/
4/5 3/6
T 11 B
x y z

T: Tree Edge B: Back Edge F: Forward Edge C: Cross Edge

Prof.Riddhi kotak Thursday, July 25, 2019


Classification of Edges in
69
DFS
 If G is undirected
 A DFS produces only tree and back edges

 A Graph is acyclic iff a DFS yields no


back edges
 Thus, can run DFS to find cycles

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
70

 We have a set of tasks and a set of


dependencies (precedence constraints)
of form “task A must be done before
task B”

 Topological sort: An ordering of the


tasks that conforms with the given
dependencies.
Prof.Riddhi kotak Thursday, July 25, 2019
Topological Sort
71

 Example:
 Resolving dependencies:
 apt-get uses topological sorting to obtain the
admissible sequence in which a set of
Debian packages can be installed/removed.

 Compilation:
 during compilation to order
modules/libraries

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
72
 Example:
 Scheduling:
 When scheduling task graphs in distributed
systems, usually we first need to sort the
tasks topologically ... and then assign them
to resources.
u w

x v z

y
Prof.Riddhi kotak Thursday, July 25, 2019
Topological Sort
73

 Topological sort more formally:


 Suppose that in a directed graph G = (V, E) vertices V
represent tasks, and each edge (u, v) ∊ E means that
task u must be done before task v.
 What is an ordering of vertices 1, ..., |V| such that for
every edge (u, v), u appears before v in the ordering?
 Such an ordering is called a topological sort of G.
 Note: there can be multiple topological sorts of G.

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
74

 Topological sort more formally:


 Is it possible to execute all the tasks in G in
an order that respects all the precedence
requirements given by the graph edges?
 The answer is "yes" if and only if the
directed graph G has no cycle!
(otherwise we have a deadlock)
 Such a G is called a Directed Acyclic Graph, or
just a DAG.

Prof.Riddhi kotak Thursday, July 25, 2019


Directed Acyclic Graphs
75

 A directed acyclic graph or DAG is a


directed graph with no directed cycles.
u w
x

x z
y z

 A directed graph G is acyclic iff a DFS of G


yields no back edges.
Prof.Riddhi kotak Thursday, July 25, 2019
Topological Sort
76

 Topological sort of a DAG:


 Linear ordering of all vertices in graph G
 such that vertex u comes before vertex v if
edge (u, v)  G.

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
77
TOPOLOGICAL-SORT(G):
1.call DFS(G) to compute finishing times f[v] for each
vertex v
2.as each vertex is finished, insert it onto the front of a
linked list
3.return the linked list of vertices

Note that the result is just a list of vertices


in order of decreasing finish times f[]
Prof.Riddhi kotak Thursday, July 25, 2019
Topological Sort
78

b c

d e

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
79

time: 0
b c

d e

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
80

time: 1
b 1/ c

d e

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
81

time: 2
b 1/ c

2/
d e

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
82

time: 3
b 1/ c

2/
d e

3/

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
83

time: 4
b 1/ c

2/
d e

3/4 f
f

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
84

time: 5
b 1/ c

2/5
d e

3/4 d f
f

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
85

time: 6
b 1/ c

2/5 6/
d e

3/4 d f
f

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
86

time: 7
b 1/ c

2/5 6/7
d e

3/4 e d f
f

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
87

time: 8
b 1/8 c

2/5 6/7
d e

3/4 c e d f
f

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
88
9/

time: 9
b 1/8 c

2/5 6/7
d e

3/4 c e d f
f

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
89
9/

time: 10
b 10/ 1/8 c

2/5 6/7
d e

3/4 c e d f
f

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
90
9/

time: 11
10/
b 1/8 c
11

2/5 6/7
d e

3/4 b c e d f
f

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
91
9/
12

time: 12
10/
b 1/8 c
11

2/5 6/7
d e

3/4 a b c e d f
f

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
a
92
9/
12
a b c e d f
1 1
8 7 5 4
10/ 2 1
b 1/8 c
11

2/5 6/7
e
The linked list is sorted
d
in decreasing order of
finishing times f[]
3/4

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort
93

 Running time of topological sort: (V+E)

 Why?
 Depth first search takes (V+E) time in the
worst case,
 and inserting into the front of a linked list
takes (1) time

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort - Example
94

b g

a e i

c h

Prof.Riddhi kotak Thursday, July 25, 2019


Topological Sort - Example
95  Topological sort is not unique.
 The following are all topological sort of the graph below:

b
s1 = {a, b, c, d, e, f, g, h, i}
g

s2 = {a, c, b, f, e, d, h, g, i}
a e i
s3 = {a, b, d, c, e, g, f, h, i}

c h s4 = {a, c, f, b, e, h, d, g, i}

etc.
f
Prof.Riddhi kotak Thursday, July 25, 2019
Connected components
96

Prof.Riddhi kotak Thursday, July 25, 2019


Connected components
97

r s t u

v w x y

Prof.Riddhi kotak Thursday, July 25, 2019


Connected components
98

STRONGLY-CONNECTED-COMPONENTs(G)
1.call DFS(G) to compute finishing times f[u] for each

vertex u
2.compute GT

3.call DFS(GT),but in the main loop of DFS, consider the

vertices in order of decreasing f[u] (as computed in line 1)


4.output the vertices of each tree in the depth-first forest

formed in line 3 as a separate strongly connected


component

Prof.Riddhi kotak Thursday, July 25, 2019


Connected components
99

r s t u

v w x y

1. call DFS(G) to compute finishing times f[u] for each


vertex u
Prof.Riddhi kotak Thursday, July 25, 2019
Connected components
100
r s t u
13/ 11/ 1/
8/9
14 16 10

12/
3/4 2/7 5/6
15
v w x y

2. compute GT
Prof.Riddhi kotak Thursday, July 25, 2019
Connected components
101
r s t u
13/ 11/ 1/
8/9
14 16 10

12/
3/4 2/7 5/6
15
v w x y

3. call DFS(GT),but in the main loop of DFS, consider the


vertices in order of decreasing f[u] (as computed in line
1)
4. output the vertices of each tree in the depth-first forest
formed in line 3 as a separate strongly connected
component
Prof.Riddhi kotak Thursday, July 25, 2019
Connected components
102
r s t u
13/ 11/ 1/
8/9
14 16 10

12/
3/4 2/7 5/6
15
v w x y

Prof.Riddhi kotak Thursday, July 25, 2019


Connected components
103

r s t u

v w x y

Prof.Riddhi kotak Thursday, July 25, 2019


Graphs and Games:
104
Introduction
 Navigation Graph
 Dependency Graph
 State Graph
 etc…..

Prof.Riddhi kotak Thursday, July 25, 2019


NIM Game
105

 Nim is a mathematical game of strategy.


 Here two players take turns removing
objects from distinct heaps.
 On each turn, a player must remove at
least one object, and may remove any
number of objects provided they all
come from the same heap.
 The goal of the game is to be the player
to remove the last object.
Prof.Riddhi kotak Thursday, July 25, 2019
Variation of NIM
106

 Consider following game


 Two players playing with single heap(pile) of
matches
 pile contains at least two matches
 Rules
 First player must remove at least one match and
there must be at least one match remains on
heap. Only for 1st move.
 2nd player must remove at least one and at most
twice the number drawn by 1st player.
 Who picks last match wins.

Prof.Riddhi kotak Thursday, July 25, 2019


Graphs and Games
107

Prof.Riddhi kotak Thursday, July 25, 2019


Graphs and Games
108

5, 2, 4,
4 2 2

3, 0, 3,
3 0 2

1,
1

i,j i,j means: i are left and can take up to j.


Prof.Riddhi kotak Thursday, July 25, 2019
Graphs and Games
109

 (0,0) is a losing position.


 A winning position has at least one
transition to a losing position.
 (n,n) has a transition to (0,0)
 A losing position has all transition to a
winning position.

Prof.Riddhi kotak Thursday, July 25, 2019


Other Games
110

 Tic tac toe


 Tower of Hanoi
 etc.
 Harder Games:
 Chess, proofs, life (the real one)
 Can’t build the entire graph.
 Nodes: position in the game
 Edges: legal moves between two positions

Prof.Riddhi kotak Thursday, July 25, 2019


111 Thank You.

Prof.Riddhi kotak Thursday, July 25, 2019

You might also like