Upload
jatin-chaudhari
View
237
Download
1
Embed Size (px)
Citation preview
7/27/2019 Chapter9 Graph Algorithm
1/125
Graph Algorithms
Chapter 9
7/27/2019 Chapter9 Graph Algorithm
2/125
Graph Concepts
*A graph is a collection of vertices (nodes)
and lines that connect pairs of vertices
* Each node may have multiple predecessors
and successors
*A directed graph (digraph) has vertices that
are connected through directional lines or
arcs
*An undirected graph has vertices connected
through non-directional lines or edges
7/27/2019 Chapter9 Graph Algorithm
3/125
G = (V, E)
a vertex may have:0 or more predecessors0 or more successors
7/27/2019 Chapter9 Graph Algorithm
4/125
some problems that can be
represented by a graph
* computer networks
* airline flights
* road map* course prerequisite structure
* tasks for completing a job
* flow of control through a program* many more
7/27/2019 Chapter9 Graph Algorithm
5/125
7/27/2019 Chapter9 Graph Algorithm
6/125
More Graph Concepts
* Two vertices are adjacent if an edge directly
connects them
* A path is a sequence of adjacent vertices* A cycle is a path that starts and ends with the
same vertex
*
Two vertices are connected if there is a pathbetween them
7/27/2019 Chapter9 Graph Algorithm
7/125
Even More Graph Concepts...
*
A digraph is strongly connected if there is a pathfrom each vertex to every other vertex
* A digraph is weakly connected if at least two vertices
are not connected
* A graph is disjoint if it is not connected
* The degree is the number of lines entering orleaving the vertex in a digraph* In-degree is the number of arcs entering the vertex
* Out-degree is the number of arcs leaving the vertex
7/27/2019 Chapter9 Graph Algorithm
8/125
7/27/2019 Chapter9 Graph Algorithm
9/125
Graph Storage Structures
* Graph data is stored in two sets of data:* Vertices (nodes)
* Edges (lines) or Arcs
*
Arrays or linked lists may be used to storedata
* Two approaches to store graph data:
* Adjacency Matrix
* Adjacency List
7/27/2019 Chapter9 Graph Algorithm
10/125
Adjacency Matrix
* Uses a vector (one-dimensional array for
vertices, and a matrix (two-dimensional array)
to store edges* Size of graph must be known before program
starts
* Only one edge can be stored between any
two vertices
7/27/2019 Chapter9 Graph Algorithm
11/125
b
7/27/2019 Chapter9 Graph Algorithm
12/125
Adjacency List
* Uses a two-dimensional array to store edges
* Vertex list is a linked list of vertices in the list, and
has a head pointer to a linked list of edges from thevertex
* The edge (arc) list is a linked list of edges (arcs);
each node in this list of arcs has a pointer to its
destination vertex
* Suitable forsparse graphs
7/27/2019 Chapter9 Graph Algorithm
13/125
* A directed path is a sequence of vertices(v0, v1, . . . , vk)
* Such that (vi, vi+1) is an arc* A directed cycleis a directed path such that the first
and last vertices are the same.
* A directed graph is acyclic if it does not contain any
directed cycles
7/27/2019 Chapter9 Graph Algorithm
14/125
Directed Graphs Usage
* Directed graphs are often used to represent order-
dependent tasks
* That is we cannot start a task before another task finishes
* We can model this task dependent constraint using arcs
* An arc (i,j) means task jcannot start until task iis finished
* Clearly, for the system not to hang, the graph must be
acyclic
i j
Task j cannot start
until task i is finished
T l i l S t Al ith
7/27/2019 Chapter9 Graph Algorithm
15/125
Topological Sort Algorithm* Topological sort is an algorithm for a directed acyclic graph
* Linearly order the vertices so that the linear order respects the
ordering relations implied by the arcs
* Observations
* Starting point must have zero indegree
* If it doesnt exist, the graph would not be acyclic
* Algorithm
1. A vertex with zero indegree is a task that can start right away.
So we can output it first in the linear order
2. If a vertex iis output, then its outgoing arcs (i, j) are no longeruseful, since tasksjdoes not need to wait fori anymore- so
remove all is outgoing arcs
3. With vertex iremoved, the new graph is still a directed acyclic
graph. So, repeat step 1-2 until no vertex is left.
7/27/2019 Chapter9 Graph Algorithm
16/125
Topological Sort
Find all starting points
Reduce indegree(w)
Place new start
vertices on the Q
7/27/2019 Chapter9 Graph Algorithm
17/125
Example
01
2
3
45
6
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
1
2
1
1
2
1
1
2
2
Indegree
start
Q = { 0 }
OUTPUT: 0
7/27/2019 Chapter9 Graph Algorithm
18/125
0
1
2
3
45
6
7
8
9
0
12
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
12
3
4
5
6
7
8
9
0
12
1
1
2
1
1
2
2
Indegree
Dequeue 0 Q = { }-> remove 0s arcs adjust
indegrees of neighbors
OUTPUT: 0
Decrement 0s
neighbors
-1
-1
-1
7/27/2019 Chapter9 Graph Algorithm
19/125
0
1
2
3
45
6
7
8
9
0
12
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
12
3
4
5
6
7
8
9
0
02
1
0
2
0
1
2
2
Indegree
Q = { 6, 1, 4 }Enqueue all starting points
OUTPUT: 0
Enqueue all
new start points
7/27/2019 Chapter9 Graph Algorithm
20/125
12
3
45
6
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
2
1
0
2
0
1
2
2
Indegree
Dequeue 6 Q = { 1, 4 }Remove arcs .. Adjust indegrees
of neighbors
OUTPUT: 0 6
Adjust neighbors
indegree
-1
-1
7/27/2019 Chapter9 Graph Algorithm
21/125
12
3
45
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
1
0
0
2
0
1
2
2
Indegree
Q = { 1, 4, 3 }
Enqueue 3
OUTPUT: 0 6
Enqueue new
start
7/27/2019 Chapter9 Graph Algorithm
22/125
12
3
45
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
1
0
0
2
0
1
2
2
Indegree
Dequeue 1 Q = { 4, 3 }Adjust indegrees of neighbors
OUTPUT: 0 6 1
Adjust neighbors
of 1
-1
7/27/2019 Chapter9 Graph Algorithm
23/125
2
3
45
7
8
9
0
1
2
3
4
56
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
56
7
8
9
0
0
0
0
0
20
1
2
2
Indegree
Dequeue 1 Q = { 4, 3, 2 }Enqueue 2
OUTPUT: 0 6 1
Enqueue new
starting points
7/27/2019 Chapter9 Graph Algorithm
24/125
2
3
45
7
8
9
0
1
2
3
4
56
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
56
7
8
9
0
0
0
0
0
20
1
2
2
Indegree
Dequeue 4 Q = { 3, 2 }Adjust indegrees of neighbors
OUTPUT: 0 6 1 4
Adjust 4s
neighbors
-1
7/27/2019 Chapter9 Graph Algorithm
25/125
2
3
5
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
1
0
1
2
2
Indegree
Dequeue 4 Q = { 3, 2 }No new start points found
OUTPUT: 0 6 1 4
NO new start
points
7/27/2019 Chapter9 Graph Algorithm
26/125
2
3
5
7
8
9
01
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
01
2
3
4
5
6
7
8
9
00
0
0
0
1
0
1
2
2
Indegree
Dequeue 3 Q = { 2 }
Adjust 3s neighbors
OUTPUT: 0 6 1 4 3
-1
7/27/2019 Chapter9 Graph Algorithm
27/125
2
5
7
8
9
0
1
2
3
4
56
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
56
7
8
9
0
0
0
0
0
10
1
1
2
Indegree
Dequeue 3 Q = { 2 }No new start points found
OUTPUT: 0 6 1 4 3
7/27/2019 Chapter9 Graph Algorithm
28/125
2
5
7
8
9
0
12
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
12
3
4
5
6
7
8
9
0
00
0
0
1
0
1
1
2
Indegree
Dequeue 2 Q = { }Adjust 2s neighbors
OUTPUT: 0 6 1 4 3 2
-1
-1
7/27/2019 Chapter9 Graph Algorithm
29/125
5
7
8
9
0
12
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
12
3
4
5
6
7
8
9
0
00
0
0
0
0
0
1
2
Indegree
Dequeue 2 Q = { 5, 7 }Enqueue 5, 7
OUTPUT: 0 6 1 4 3 2
7/27/2019 Chapter9 Graph Algorithm
30/125
5
7
8
9
01
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
01
2
3
4
5
6
7
8
9
00
0
0
0
0
0
0
1
2
Indegree
Dequeue 5 Q = { 7 }
Adjust neighbors
OUTPUT: 0 6 1 4 3 2 5
-1
7/27/2019 Chapter9 Graph Algorithm
31/125
7
8
9
01
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
01
2
3
4
5
6
7
8
9
00
0
0
0
0
0
0
1
1
Indegree
Dequeue 5 Q = { 7 }
No new starts
OUTPUT: 0 6 1 4 3 2 5
7/27/2019 Chapter9 Graph Algorithm
32/125
7
8
9
01
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
01
2
3
4
5
6
7
8
9
00
0
0
0
0
0
0
1
1
Indegree
Dequeue 7 Q = { }
Adjust neighbors
OUTPUT: 0 6 1 4 3 2 5 7
-1
7/27/2019 Chapter9 Graph Algorithm
33/125
8
9
01
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
01
2
3
4
5
6
7
8
9
00
0
0
0
0
0
0
0
1
Indegree
Dequeue 7 Q = { 8 }
Enqueue 8
OUTPUT: 0 6 1 4 3 2 5 7
7/27/2019 Chapter9 Graph Algorithm
34/125
8
9
01
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
01
2
3
4
5
6
7
8
9
00
0
0
0
0
0
0
0
1
Indegree
Dequeue 8 Q = { }
Adjust indegrees of neighbors
OUTPUT: 0 6 1 4 3 2 5 7 8
-1
7/27/2019 Chapter9 Graph Algorithm
35/125
9
0
1
2
3
4
56
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
56
7
8
9
0
0
0
0
0
00
0
0
0
Indegree
Dequeue 8 Q = { 9 }
Enqueue 9
Dequeue 9 Q = { }
STOP no neighbors
OUTPUT: 0 6 1 4 3 2 5 7 8 9
7/27/2019 Chapter9 Graph Algorithm
36/125
OUTPUT: 0 6 1 4 3 2 5 7 8 9
0
1
2
3
45
6
7
8
9
Is output topologically correct?
7/27/2019 Chapter9 Graph Algorithm
37/125
Topological Sort: Complexity
We never visited a vertex more than one time
For each vertex, we had to examine all outgoing
edges
outdegree(v) = m
This is summed over all vertices, not per vertex
So, our running time is exactly O(n + m)
7/27/2019 Chapter9 Graph Algorithm
38/125
Weighted Graphs
* In many applications, each edge of a graph has an
associated numerical value, called a weight.
* Usually, the edge weights are nonnegative integers.
* Weighted graphs may be either directed or undirected
*
If any edge has a negative weight then the shortestpath between the vertices will be undefined and well
get into the loop, this loop is known as negative cost
loop
7/27/2019 Chapter9 Graph Algorithm
39/125
Graph Traversal
* Application example
* Given a graph representation and a vertex s in the
graph
* Find all paths from s to other vertices
* Two common graph traversal algorithmsBreadth-First Search (BFS)
Find the shortest paths in an unweighted graph
Depth-First Search (DFS)
Topological sort
Find strongly connected components
Lets first look at BFS . . .
7/27/2019 Chapter9 Graph Algorithm
40/125
Breadth-First Search: Properties
BFS calculates the shortest-path distance to
the source node
Shortest-path distance(s,v) = minimum number ofedges from s to v, or if v not reachable from s
BFS builds breadth-first tree, in which paths to
root represent shortest paths in G
Thus can use BFS to calculate shortest path fromone vertex to another in O(V+E) time
7/27/2019 Chapter9 Graph Algorithm
41/125
BFS and Shortest Path Problem
Given any source vertex s, BFS visits the other
vertices at increasing distances away from s. In doing
so, BFS discovers paths from s to other vertices
What do we mean by distance? The number ofedges on a path from s.
2
4
3
5
1
7
6
9
8
0
Consider s=vertex 1
Nodes at distance 1?
2, 3, 7, 91
1
1
12
22
2
s
Example
Nodes at distance 2?
8, 6, 5, 4
Nodes at distance 3?
0
7/27/2019 Chapter9 Graph Algorithm
42/125
BSF algorithm
Why use queue? Need FIFO
7/27/2019 Chapter9 Graph Algorithm
43/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
F
F
F
F
F
F
F
F
Q = { }
Initialize visitedtable (all False)
Initialize Q to be empty
7/27/2019 Chapter9 Graph Algorithm
44/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
T
F
F
F
F
F
F
F
Q = { 2 }
Flag that 2 hasbeen visited.
Place source 2 on the queue.
7/27/2019 Chapter9 Graph Algorithm
45/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
T
T
F
T
F
F
F
T
F
Q = {2} { 8, 1, 4 }
Mark neighbors
as visited.
Dequeue 2.
Place all unvisited neighbors of 2 on the queue
Neighbors
7/27/2019 Chapter9 Graph Algorithm
46/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
F
T
F
F
F
T
T
Q = { 8, 1, 4 } { 1, 4, 0, 9 }
Mark new visited
Neighbors.
Dequeue 8.
-- Place all unvisited neighbors of 8 on the queue.
-- Notice that 2 is not placed on the queue again, it has been visited!
Neighbors
7/27/2019 Chapter9 Graph Algorithm
47/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 1, 4, 0, 9 } { 4, 0, 9, 3, 7 }
Mark new visited
Neighbors.
Dequeue 1.
-- Place all unvisited neighbors of 1 on the queue.
-- Only nodes 3 and 7 havent been visited yet.
Neighbors
7/27/2019 Chapter9 Graph Algorithm
48/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 4, 0, 9, 3, 7 } { 0, 9, 3, 7 }
Dequeue 4.
-- 4 has no unvisited neighbors!
Neighbors
7/27/2019 Chapter9 Graph Algorithm
49/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 0, 9, 3, 7 } { 9, 3, 7 }
Dequeue 0.
-- 0 has no unvisited neighbors!
Neighbors
7/27/2019 Chapter9 Graph Algorithm
50/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 9, 3, 7 } { 3, 7 }
Dequeue 9.
-- 9 has no unvisited neighbors!
Neighbors
7/27/2019 Chapter9 Graph Algorithm
51/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
F
T
T
T
Q = { 3, 7 } { 7, 5 }
Dequeue 3.
-- place neighbor 5 on the queue.
Neighbors
Mark new visited
Vertex 5.
7/27/2019 Chapter9 Graph Algorithm
52/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 7, 5 } { 5, 6 }
Dequeue 7.
-- place neighbor 6 on the queue.
Neighbors
Mark new visited
Vertex 6.
7/27/2019 Chapter9 Graph Algorithm
53/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 5, 6} { 6 }
Dequeue 5.
-- no unvisited neighbors of 5.
Neighbors
7/27/2019 Chapter9 Graph Algorithm
54/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 6 } { }
Dequeue 6.
-- no unvisited neighbors of 6.
Neighbors
7/27/2019 Chapter9 Graph Algorithm
55/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { } STOP!!! Q is empty!!!There exists a path from source
vertex 2 to all vertices in the graph.
7/27/2019 Chapter9 Graph Algorithm
56/125
Shortest Path Recording
BFS we saw only tells us whether a path exists fromsource s, to other vertices v. It doesnt tell us the path!
We need to modify the algorithm to record the path.
How can we do that? Note: we do not know which vertices lie on this path until we
reach v!
Efficient solution:Use an additional arraypred[0..n-1]
Pred[w] = v means that vertex w was visitedfrom v
BFS P h Fi di
7/27/2019 Chapter9 Graph Algorithm
57/125
BFS + Path Finding
initialize all pred[v] to -1
Record where you
came from
7/27/2019 Chapter9 Graph Algorithm
58/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
F
F
F
F
F
F
F
F
Q = { }
Initialize visitedtable (all False)
Initialize Pred to -1
Initialize Q to be empty
-
-
-
-
-
-
-
-
-
-
Pred
7/27/2019 Chapter9 Graph Algorithm
59/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
T
F
F
F
F
F
F
F
Q = { 2 }
Flag that 2 hasbeen visited.
Place source 2 on the queue.
-
-
-
-
-
-
-
-
-
-
Pred
7/27/2019 Chapter9 Graph Algorithm
60/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
T
T
F
T
F
F
F
T
F
Q = {2} { 8, 1, 4 }
Mark neighborsas visited.
Record in Pred
that we came
from 2.Dequeue 2.
Place all unvisited neighbors of 2 on the queue
Neighbors
-
2
-
-
2
-
-
-
2
-
Pred
7/27/2019 Chapter9 Graph Algorithm
61/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
F
T
F
F
F
T
T
Q = { 8, 1, 4 } { 1, 4, 0, 9 }
Mark new visited
Neighbors.
Record in Pred
that we came
from 8.Dequeue 8.
-- Place all unvisited neighbors of 8 on the queue.
-- Notice that 2 is not placed on the queue again, it has been visited!
Neighbors
8
2
-
-
2
-
-
-
2
8
Pred
7/27/2019 Chapter9 Graph Algorithm
62/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 1, 4, 0, 9 } { 4, 0, 9, 3, 7 }
Mark new visited
Neighbors.
Record in Pred
that we came
from 1.Dequeue 1.
-- Place all unvisited neighbors of 1 on the queue.
-- Only nodes 3 and 7 havent been visited yet.
Neighbors
8
2
-
1
2
-
-
1
2
8
Pred
7/27/2019 Chapter9 Graph Algorithm
63/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 4, 0, 9, 3, 7 } { 0, 9, 3, 7 }
Dequeue 4.
-- 4 has no unvisited neighbors!
Neighbors
8
2
-
1
2
-
-
1
2
8
Pred
7/27/2019 Chapter9 Graph Algorithm
64/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 0, 9, 3, 7 } { 9, 3, 7 }
Dequeue 0.
-- 0 has no unvisited neighbors!
Neighbors
8
2
-
1
2
-
-
1
2
8
Pred
7/27/2019 Chapter9 Graph Algorithm
65/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 9, 3, 7 } { 3, 7 }
Dequeue 9.
-- 9 has no unvisited neighbors!
Neighbors
8
2
-
1
2
-
-
1
2
8
Pred
7/27/2019 Chapter9 Graph Algorithm
66/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
F
T
T
T
Q = { 3, 7 } { 7, 5 }
Dequeue 3.
-- place neighbor 5 on the queue.
Neighbors
Mark new visitedVertex 5.
Record in Pred
that we came
from 3.
8
2
-
1
2
3
-
1
2
8
Pred
7/27/2019 Chapter9 Graph Algorithm
67/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 7, 5 } { 5, 6 }
Dequeue 7.
-- place neighbor 6 on the queue.
Neighbors
Mark new visitedVertex 6.
Record in Pred
that we came
from 7.
8
2
-
1
2
3
7
1
2
8
Pred
7/27/2019 Chapter9 Graph Algorithm
68/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 5, 6} { 6 }
Dequeue 5.
-- no unvisited neighbors of 5.
Neighbors
8
2
-
1
2
3
7
1
2
8
Pred
7/27/2019 Chapter9 Graph Algorithm
69/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 6 } { }
Dequeue 6.
-- no unvisited neighbors of 6.
Neighbors
8
2
-
1
2
3
7
1
2
8
Pred
7/27/2019 Chapter9 Graph Algorithm
70/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { } STOP!!! Q is empty!!!
Pred now can be traced backward
to report the path!
8
2
-
1
2
3
7
1
2
8
Pred
How do we record the shortest
7/27/2019 Chapter9 Graph Algorithm
71/125
How do we record the shortest
distances?
d(v) = ;
d(w)=d(v)+1;
d(s) = 0;
7/27/2019 Chapter9 Graph Algorithm
72/125
Application of BFS
One application concerns how to find
connected components in a graph
If a graph has more than one connected
components, BFS builds a BFS-forest (not just
BFS-tree)!
Each tree in the forest is a connected component.
7/27/2019 Chapter9 Graph Algorithm
73/125
Depth-First Search (DFS)
DFS is another popular graph search strategy Idea is similar to pre-order traversal (visit children
first)
DFS can provide certain information aboutthe graph that BFS cannot It can tell whether we have encountered a cycle or
not
Depth-First Search
7/27/2019 Chapter9 Graph Algorithm
74/125
p
Depth-first search is another strategy for exploring agraph
Explore deeper in the graph whenever possible
Edges are explored out of the most recently discovered
vertex vthat still has unexplored edges When all ofvs edges have been explored, backtrack to the
vertex from which vwas discovered
DFS will continue to visit neighbors in a recursive
pattern Whenever we visit v from u, we recursively visit all unvisited
neighbors of v. Then we backtrack (return) to u.
7/27/2019 Chapter9 Graph Algorithm
75/125
DFS Algorithm
Flag all vertices as not
visited
Flag yourself as visited
For unvisited neighbors,call RDFS(w) recursively
We can also record the paths using pred[ ].
E ample
7/27/2019 Chapter9 Graph Algorithm
76/125
Example
2
4
3
5
1
76
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
F
F
F
F
F
F
F
F
Initialize visitedtable (all False)
Initialize Pred to -1
-
-
-
-
-
-
-
-
-
-
Pred
Example
7/27/2019 Chapter9 Graph Algorithm
77/125
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
T
F
F
F
F
F
F
F
Mark 2 as visited
-
-
-
-
-
-
-
-
-
-
Pred
RDFS( 2 )
Now visit RDFS(8)
Example
7/27/2019 Chapter9 Graph Algorithm
78/125
Example
2
4
3
5
1
7
6
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
T
F
F
F
F
F
T
F
Mark 8 as visited
mark Pred[8]
-
-
-
-
-
-
-
-
2
-
Pred
RDFS( 2 )
RDFS(8)
2 is already visited, so visit RDFS(0)
Recursive
calls
Example
7/27/2019 Chapter9 Graph Algorithm
79/125
Example
2
4
3
5
1
7
6
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
F
T
F
F
F
F
F
T
F
Mark 0 as visited
Mark Pred[0]
8
-
-
-
-
-
-
-
2
-
Pred
RDFS( 2 )
RDFS(8)
RDFS(0) -> no unvisited neighbors, return
to call RDFS(8)
Recursive
calls
Example
7/27/2019 Chapter9 Graph Algorithm
80/125
Example
2
4
3
5
1
7
6
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
F
T
F
F
F
F
F
T
F
8
-
-
-
-
-
-
-
2
-
Pred
RDFS( 2 )
RDFS(8)
Now visit 9 -> RDFS(9)
Recursive
calls
Back to 8
Example
7/27/2019 Chapter9 Graph Algorithm
81/125
Example
2
4
3
5
1
7
6
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
F
T
F
F
F
F
F
T
T
Mark 9 as visited
Mark Pred[9]
8
-
-
-
-
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
-> visit 1, RDFS(1)
Recursive
calls
Example
7/27/2019 Chapter9 Graph Algorithm
82/125
Example
2
4
3
5
1
7
6
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
F
F
F
F
F
T
T
Mark 1 as visited
Mark Pred[1]
8
9
-
-
-
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
visit RDFS(3)
Recursive
calls
Example
7/27/2019 Chapter9 Graph Algorithm
83/125
Example
2
4
3
5
1
7
6
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
F
F
F
F
T
T
Mark 3 as visited
Mark Pred[3]
8
9
-
1
-
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
visit RDFS(4)
Recursive
calls
Example
7/27/2019 Chapter9 Graph Algorithm
84/125
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(4)STOP all of 4s neighbors have been visited
return back to call RDFS(3)
Example
2
43
5
1
7
6
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
F
T
T
Mark 4 as visited
Mark Pred[4]
8
9
-
1
3
-
-
-
2
8
Pred
Recursive
calls
Example
7/27/2019 Chapter9 Graph Algorithm
85/125
Example
2
43
5
1
7
6
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
F
T
T
8
9
-
1
3
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
visit 5 -> RDFS(5)
Recursive
calls
Back to 3
Example
7/27/2019 Chapter9 Graph Algorithm
86/125
Example
2
43
5
1
7
6
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
F
F
T
T
8
9
-
1
3
3
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
3 is already visited, so visit 6 -> RDFS(6)
Recursive
calls
Mark 5 as visited
Mark Pred[5]
Example
7/27/2019 Chapter9 Graph Algorithm
87/125
Example
2
43
5
1
7
6
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
F
T
T
8
9
-
1
3
3
5
-
2
8
PredRDFS( 2 )
RDFS(8)RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6)
visit 7 -> RDFS(7)
Recursive
calls
Mark 6 as visited
Mark Pred[6]
Example
7/27/2019 Chapter9 Graph Algorithm
88/125
Example
2
43
5
1
7
6
9
8
0Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
PredRDFS( 2 )
RDFS(8)RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6)
RDFS(7) -> Stop no more unvisited neighbors
Recursive
calls
Mark 7 as visited
Mark Pred[7]
Example
7/27/2019 Chapter9 Graph Algorithm
89/125
ExampleAdjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
PredRDFS( 2 )
RDFS(8)RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6) -> Stop
Recursive
calls
2
4
3
5
1
76
9
8
0
source
Example
7/27/2019 Chapter9 Graph Algorithm
90/125
ExampleAdjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
PredRDFS( 2 )
RDFS(8)RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5) -> Stop
Recursive
calls
2
4
3
5
1
76
9
8
0
source
Example
7/27/2019 Chapter9 Graph Algorithm
91/125
ExampleAdjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
PredRDFS( 2 )
RDFS(8)RDFS(9)
RDFS(1)
RDFS(3) -> Stop
Recursive
calls
2
4
3
5
1
76
9
8
0
source
Example
7/27/2019 Chapter9 Graph Algorithm
92/125
ExampleAdjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
PredRDFS( 2 )
RDFS(8)RDFS(9)
RDFS(1) -> Stop
Recursive
calls
2
4
3
5
1
76
9
8
0
source
Example
7/27/2019 Chapter9 Graph Algorithm
93/125
ExampleAdjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
PredRDFS( 2 )
RDFS(8)RDFS(9) -> StopRecursive
calls
2
4
3
5
1
76
9
8
0
source
Example
7/27/2019 Chapter9 Graph Algorithm
94/125
ExampleAdjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
PredRDFS( 2 )
RDFS(8) -> StopRecursive
calls
2
4
3
5
1
76
9
8
0
source
Example
7/27/2019 Chapter9 Graph Algorithm
95/125
ExampleAdjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
PredRDFS( 2 ) -> Stop
Recursive
calls
2
4
3
5
1
76
9
8
0
source
Example
7/27/2019 Chapter9 Graph Algorithm
96/125
pAdjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
Pred
Try some examples.
Path(0) ->
Path(6) ->
Path(7) ->
Check our paths, does DFS find valid paths? Yes.
2
4
3
5
1
7
6
9
8
0
source
Time Complexity of DFS
7/27/2019 Chapter9 Graph Algorithm
97/125
p y(Using adjacency list)
We never visited a vertex more than once
We had to examine all edges of the vertices
We know vertex vdegree(v) = 2m where m is the number of
edges
So, the running time of DFS is proportional to the
number of edges and number of vertices (same as BFS)
O(n + m)
You will also see this written as:
O(|v|+|e|) |v| = number of vertices (n)
|e| = number of edges (m)
G d Al i h
7/27/2019 Chapter9 Graph Algorithm
98/125
Greedy Algorithms
Suppose your task is to optimize something, and you
write an algorithm that proceeds in steps to solve this
task.
Each step you have a decision to make on how toproceed in the algorithm.
A greedy algorithm is one where you make the
decision that would appearto be the best for
eventually optimizing the problem.
Example
7/27/2019 Chapter9 Graph Algorithm
99/125
For the shortest-path graph problem, we actually
used a greedy algorithm.
This was Dijkstras algorithm.
Greedy algorithms are common in graph problems.
Greedy algorithms may not always work, because
although the greedy approach might be best in theshort term, it might not be best in the long term
(when you finally reach the conclusion of the
algorithm).
For example, Dijkstras algorithm can fail if any edge
cost is negative.
N k Fl P bl
7/27/2019 Chapter9 Graph Algorithm
100/125
Network Flow Problems
Consider the graph G to be a network, and the costs
on edges to be flow capacities.
We have two special vertices: a source s and a sink t.
At the other vertices the flow coming in must equal theflow going out.
Maximum flow problem: find the maximum amount of
flow that can pass from s to t.
N k Fl A li i
7/27/2019 Chapter9 Graph Algorithm
101/125
Network Flow Applications
We could be representing the amount of water
than can flow through a network of pipes
each pipe may have a different capacity.
Or, this could be a network of streets and
each street can handle a different number of
vehicles.
E l
7/27/2019 Chapter9 Graph Algorithm
102/125
Example
s
t
a b
c d
3 2
1
43
2
32
s
t
a b
c d
3 2
0
12
2
32
The left graph shows the capacity of the edges, while the right graph shows
the maximum flow through this network. How do we obtain this? With a
greedy algorithm!
Maximum Flow Algorithm
7/27/2019 Chapter9 Graph Algorithm
103/125
We use three graphs, the original graph G, a flowgraph G
fand a residual graph G
r= G G
f.
We proceed in stages. Each stage we choose a pathin Gr from s to t. The minimum edge on this path is theamount of flow that can be added to every edge on
that path.An augmenting path is a directed path from the source
to the sink in the residual network such that every arcon this path has positive residual capacity.
We do this by adjusting Gfand recomputing Gr .
We continue until there are no paths from s to t.
We cant follow any edges that have capacity 0.
E l
7/27/2019 Chapter9 Graph Algorithm
104/125
Example
s
t
a b
c d
3 2
1
43
2
32
s
t
a b
c d
0 0
0
00
0
00
The left graph shows the capacity of the edges, the middle graph shows
the flow graph and the right graph is the residual graph.
s
t
a b
c d
3 2
1
43
2
32
E l
7/27/2019 Chapter9 Graph Algorithm
105/125
Example
s
t
a b
c d
3 2
1
43
2
32
s
t
a b
c d
0 2
0
00
2
20
Let us first choose the path (s, b, d, t) on the residual graph. The minimum
flow along that path is 2. We update the flow graph and recompute the
residual graph.
s
t
a b
c d
3 0
1
43
0
12
E l
7/27/2019 Chapter9 Graph Algorithm
106/125
Example
s
t
a b
c d
3 2
1
43
2
32
s
t
a b
c d
2 2
0
02
2
22
Now lets choose the path (s, a, c, t) on the residual graph. The minimum
flow along that path is 2. We update the flow graph and recompute the
residual graph.
s
t
a b
c d
1 0
1
41
0
10
E l
7/27/2019 Chapter9 Graph Algorithm
107/125
Example
s
t
a b
c d
3 2
1
43
2
32
s
t
a b
c d
3 2
0
12
2
32
Now lets choose the path (s, a, d, t) on the residual graph. The minimum
flow along that path is 1. We update the flow graph and recompute the
residual graph. There are no paths left, so we are done.
s
t
a b
c d
0 0
1
31
0
00
Di i
7/27/2019 Chapter9 Graph Algorithm
108/125
Discussion
In the prior example we in fact obtained the maximumflow.
If we choose a greedy algorithm, wed be tempted tochoose paths that allows the maximum amount of flowto be added at each step. This might not work.
For example, lets choose (s, a, d, t) first, because thisallows 3 units of flow to be added.
E l
7/27/2019 Chapter9 Graph Algorithm
109/125
Example
s
t
a b
c d
3 2
1
43
2
32
s
t
a b
c d
3 0
0
30
0
30
There are no paths from s to t in the residual graph, so we are done, but
we have not obtained the maximum possible flow.
s
t
a b
c d
0 2
1
13
2
02
A B tt Al ith
7/27/2019 Chapter9 Graph Algorithm
110/125
A Better Algorithm
We can make the algorithm work by allowing
the algorithm to change its mind.
In effect we allow the algorithm to undo its
decisions by sending flow back in the opposite
direction. This is best seen by example. We
have to modify the residual graph.
E ample
7/27/2019 Chapter9 Graph Algorithm
111/125
Example
s
t
a b
c d
3 2
1
43
2
32
s
t
a b
c d
3 0
0
30
0
30
We again choose the path (s, a, d, t). But note we now allow the flow to
backup in the residual graph.
s
t
a b
c d
02
1
13
2
02
3
3
3
Example
7/27/2019 Chapter9 Graph Algorithm
112/125
Example
s
t
a b
c d
3 2
1
43
2
32
s
t
a b
c d
3 2
0
12
2
32
We can now follow the path (s, b, d, a, c, t). The minimum flow along this
path is 2. Note the flow from d to a is now 1 = 32. We are done. This is
the maximum flow solution.
s
t
a b
c d
00
1
31
0
00
3
1
3
2
22
2
Conclusions
7/27/2019 Chapter9 Graph Algorithm
113/125
Conclusions
This better greedy algorithm will always find
the maximum flow solution if the edge
capacities are rational numbers.
Although we have used an acyclic graph in
the example, the algorithm works on arbitrary
graphs!
Minimum-Cost Spanning Tree
7/27/2019 Chapter9 Graph Algorithm
114/125
weighted connected undirected graph
cost of spanning tree is sum of edge costs
The minimum spanning tree (MST) of a graph defines
the cheapest subset of edges that keeps the graph in
one connected component.
Telephone companies are particularly interested inminimum spanning trees, because the minimum
spanning tree of a set of sites defines the wiring
scheme that connects the sites using as little wire as
possible.
It is the mother of all network design problems.
Spanning Tree
7/27/2019 Chapter9 Graph Algorithm
115/125
Spanning Tree
A spanning tree ofG is a subgraph which
is a tree
contains all vertices ofG
Minimum Spanning Trees
7/27/2019 Chapter9 Graph Algorithm
116/125
Minimum Spanning Trees
Undirected, connected graph G
= (V,E)
Weight function W: ER
(assigning cost or length or othervalues to edges)
( , )
( ) ( , )u v T
w T w u v
Minimum spanning tree: tree that connects allthe vertices and minimizes
Example
7/27/2019 Chapter9 Graph Algorithm
117/125
Example
Network has 10 edges.
Spanning tree has only n - 1 = 7 edges.
Need to either select 7 edges or discard 3.
1 3 5 7
2 4 6 8
2 4 63
8 10 14
127
9
Edge Selection Greedy Strategies
7/27/2019 Chapter9 Graph Algorithm
118/125
Edge Selection Greedy Strategies
Start with an n-vertex 0-edge forest. Consideredges in ascending order of cost. Select edgeif it does not form a cycle together withalready selected edges. Kruskals method.
Start with a 1-vertex tree and grow it into ann-vertex tree by repeatedly adding a vertexand an edge. When there is a choice, add aleast cost edge. Prims method.
Kruskals Method
7/27/2019 Chapter9 Graph Algorithm
119/125
us a s e od
Start with a forest that has no edges.
1 3 5 7
2 4 6 8
2 4 63
8 10 14
127
9
1 3 5 7
2 4 6 8
Consider edges in ascending order of cost.
Edge (1,2) is considered first and added to
the forest.
Kruskals Method
7/27/2019 Chapter9 Graph Algorithm
120/125
Edge (7,8)is considered next and added.
1 3 5 7
2 4 6 8
2 4 63
8 10 14
127
9
1 3 5 7
2 4 6 8
23
Edge (3,4)is considered next and added.
4
Edge (5,6)is considered next and added.
6
Edge (2,3)is considered next and added.
7
Edge (1,3)is considered next and rejected
because it creates a c cle
Kruskals Method
7/27/2019 Chapter9 Graph Algorithm
121/125
Edge (2,4)is considered next and rejected
because it creates a cycle.
1 3 5 7
2 4 6 8
2 4 63
8 10 14
127
9
1 3 5 7
2 4 6 8
234
Edge (3,5)is considered next and added.
6
10
Edge (3,6)is considered next and rejected.
7
Edge (5,7)is considered next and added.
14
Kruskals Method
7/27/2019 Chapter9 Graph Algorithm
122/125
n - 1 edges have been selected and no cycle
formed.
So we must have a spanning tree.
Cost is 46.
Min-cost spanning tree is unique when all
edge costs are different.
1 3 5 7
2 4 6 8
2 4 63
8 10 14
127
9
1 3 5 7
2 4 6 8
234 6
10
7
14
Prims Method
7/27/2019 Chapter9 Graph Algorithm
123/125
Start with any single vertex tree.
1 3 5 7
2 4 6 8
2 4 63
8 10 14
127
9
5
Get a 2-vertex tree by adding a cheapest edge.
6
6
Get a 3-vertex tree by adding a cheapest edge.
3
10
Grow the tree one edge at a time until the tree
has n - 1 edges (and hence has all n vertices).
4
4
2
71
2
7
14
8
3
Euler Circuits
7/27/2019 Chapter9 Graph Algorithm
124/125
Euler circuit, which must end on its starting vertex,
is possible only if the graph is connected and eachvertex has a even degree.
If any vertex has odd degree, then we will reach the
point where only one edge into v is unvisited.
If exactly 2 vertices have odd degree all the edges
will be visited but it will not return to its starting
vertex.
If more than 2 vertices have odd degree, then anEuler tour is not possible.
NP-complete problemA bl i ll d NP ( d t i i ti l i l) if it
7/27/2019 Chapter9 Graph Algorithm
125/125
A problem is called NP (nondeterministic polynomial) if its
solution (if one exists) can be guessed and verified in
polynomial time. Nondeterministic means that no particular rule is followed to
make the guess.
If a problem is NP and all otherNP problems are polynomial-
time reducible to it, the problem is NP-complete. Thus, finding an efficient algorithm for any NP-complete
problem implies that an efficient algorithm can be found for all
such problems.
When an NP-complete problem must be solved, one approachis to use a polynomial algorithm to approximate the solution; the
th bt i d ill t il b ti l b t ill b