Graph - The Toppers Way

advertisement
Introduction
DATA STRUCTURE – Graph
 electronic circuits
CS16
 networks (roads, flights,
communications)
JFK
LAX
HNL
STL
DFW
FTL
Graph
 A graph data structure consists of a finite (and possibly




mutable) set of nodes or vertices, together with a set of
ordered pairs of these nodes (or, in some cases, a set of
unordered pairs).
These pairs are known as edges or arcs.
As in mathematics, an edge (x,y) is said to point or go
from x to y.
The nodes may be part of the graph structure, or may be
external entities represented by integer indices or
references.
A graph data structure may also associate to each edge
some edge value, such as a symbolic label or a numeric
attribute (cost, capacity, length, etc.).
 A graph may be either undirected or directed.
 An undirected edge models a "two-way“ or "duplex"
connection between its endpoints,
 while a directed edge model is a one-way connection,
and is typically drawn as an arrow. A directed edge is
often called an arc.
 An undirected graph can have at most (N+1 / 2)edges
(one for each unordered pair), while a directed graph
can have at most edges (N^2 )(one per ordered pair).
 A multigraph can have more than one edge between
the same two vertices.
 Length of path is the number of edges connecting vertices in





the sequence of the vertices in the path.
This number is equal to the number of vertices in the path
minus one. The length of our example for path "1", "12",
"19", "21" is three.
Cost of path in a weighted graph, we call the sum of the
weights (costs) of the edges involved in the path. In real life
the road from Sofia to Madrid, for example, is equal to the
length of the road from Sofia to Paris plus the length of the
road from Madrid to Paris.
In our example, the length of the path "1", "12", "19" and "21"
is equal to 3 + 16 + 2 = 21.
Loop is a path in which the initial and the final vertex of the
path match. Example of vertices forming loop are "1", "12"
and "19". In the same time "1", "7" and "21" do not form a
loop.
Looping edge we will call an edge, which starts and ends in
the same vertex. In our example the vertex "14" is looped.
Adjacency Matrix
LinkList Representation
LinkList Representation
Operations
The basic operations provided by a graph data structure G usually
include:
 adjacent(G, x, y): tests whether there is an edge from node x to
node y.
 neighbors(G, x): lists all nodes y such that there is an edge from x
to y.
 add(G, x, y): adds to G the edge from x to y, if it is not there.
 delete(G, x, y): removes the edge from x to y, if it is there.
 get_node_value(G, x): returns the value associated with the node x.
 set_node_value(G, x, a): sets the value associated with the node x
to a.
 get_edge_value(G, x, y): returns the value associated to the edge
(x,y).
 set_edge_value(G, x, y, v): sets the value associated to the edge
(x,y) to v.
Treversing a Graph
Breadth First Search
 Breadth First Search Algorithm beginning at a start




Node A.
Examine the first Node A.
Then examine all neighbors of A .
Then examine all neighbors of neighbors of A.
And So on….
Algorithm
 Initialize all node to ready state. (Status 1)
 Put the Starting Node A in Queue and change the




status to the waiting state (Status 2)
Repeat Step 4 & 5 until Queue is empty.
Remove the front Node N of Queue. Process N and
Change the Status of N to Processed state (Status 3).
Add to rear of queue all the neighbors of N that are in
steady state (Status 1) and change their status to
waiting state (Status 2).
Exit.
Depth First Search
 Depth First Search Algorithm beginning at a start




Node A.
Examine the first Node A.
Then examine all neighbors of A .
Then examine all neighbors of neighbors of A.
And So on….
Algorithm
 Initialize all node to ready state. (Status 1)
 Push the Starting Node A onto Stack and change the




status to the waiting state (Status 2)
Repeat Step 4 & 5 until Stack is empty.
Pop the Top Node N of Stack. Process N and Change
the Status of N to Processed state (Status 3).
Push onto Stack all the neighbors of N that are in
steady state (Status 1) and change their status to
waiting state (Status 2).
Exit.
Problem: Laying Telephone Wire
Central office
Wiring: Naïve Approach
Central office
Expensive!
Wiring: Better Approach
Central office
Minimize the total length of wire connecting the customers
ORD
10
1
PIT
DEN
6
7
9
3
STL
4
8
DFW
DCA
5
2
ATL
Spanning
trees
 Suppose you have
a connected undirected graph
 Connected: every node is reachable from every other node
 Undirected: edges do not have an associated direction
 ...then a spanning tree of the graph is a connected sub-
graph in which there are no cycles
A connected,
undirected graph
Four of the spanning trees of the graph
Spanning
Trees
A spanning tree of a graph is just a subgraph that
contains all the vertices and is a tree.
A graph may have many spanning trees.
Graph
A
Some Spanning Trees from Graph A
o
r
o
r
o
r
Complete
Graph
All 16 of its Spanning Trees
Finding a spanning tree
 To find a spanning tree of a graph,
pick an initial node and call it part of the spanning tree
do a search from the initial node:
each time you find a node that is not in the spanning tree,
add to the spanning tree both the new node and the edge
you followed to get to it
An undirected graph
One possible
result of a BFS
starting from top
One possible
result of a DFS
starting from top
Minimizing costs
 Suppose you want to supply a set of houses (say, in a
new subdivision) with:
 electric power
 water
 sewage lines
 telephone lines
 To keep costs down, you could connect these houses
with a spanning tree (of, for example, power lines)
 However, the houses are not all equal distances apart
 To reduce costs even further, you could connect the
houses with a minimum-cost spanning tree
Minimum Spanning Trees
The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for
that graph.
Complete Graph
Minimum Spanning Tree
7
2
2
5
3
3
4
1
1
Minimum-cost spanning trees
 Suppose you have a connected undirected graph with a weight
(or cost) associated with each edge
 The cost of a spanning tree would be the sum of the costs of
its edges
 A minimum-cost spanning tree is a spanning tree that has the
lowest cost
A
19
16
21 11
33
E
F
18
B
5
14
D
A
6
C
10
A connected, undirected graph
16
11
F
E
18
B
5
6
C
D
A minimum-cost spanning tree
Minimum Spanning Tree (MST)
A minimum spanning tree is a subgraph of an
undirected weighted graph G, such that
• it is a tree (i.e., it is acyclic)
• it covers all the vertices V
– contains |V| - 1 edges
• the total cost associated with tree edges is the
minimum among all possible spanning trees
• not necessarily unique
How Can We Generate a MST?
9
a
2
5
4
c
6
d
4
5
9
b
5
e
a
2
5
4
c
b
6
d
4
5
5
e
Finding spanning trees
 There are two basic algorithms for finding minimum-cost
spanning trees, and both are greedy algorithms
 Kruskal’s algorithm: Start with N0 nodes or edges in the
spanning tree, and repeatedly add the cheapest edge that
does not create a cycle
 Here, we consider the spanning tree to consist of edges only
 Prim’s algorithm: Start with any one node in the spanning
tree, and repeatedly add the cheapest edge, and the node it
leads to, for which the node is not already in the spanning
tree.
 Here, we consider the spanning tree to consist of both nodes and
edges
Kruskal’s algorithm
T = empty spanning tree;
E = set of edges;
N = number of nodes in graph;
while T has fewer than N - 1 edges
{
remove an edge (v, w) of lowest cost from E
if adding (v, w) to T would create a cycle
then discard (v, w)
else add (v, w) to T
}
 Finding an edge of lowest cost can be done just by sorting the
edges
Prim’s algorithm
T = a spanning tree containing a single node s;
E = set of edges adjacent to s;
while T does not contain all the nodes
{
remove an edge (v, w) of lowest cost from E
if w is already in T then discard edge (v, w)
else
{
add edge (v, w) and node w to T
add to E the edges adjacent to w
}
}
 An edge of lowest cost can be found with a priority queue
 Testing for a cycle is automatic
 Hence, Prim’s algorithm is far simpler to implement than Kruskal’s
algorithm
Prim’s algorithm
Starting from
empty T, choose a
vertex at random
and initialize
1
10
5
1
8
2
3
3
4
V = {1), E’ ={}
1
6
1
4
6
2
5
Prim’s
algorithm
Choose the vertex u
not in V such that
edge weight from u
to a vertex in V is
minimal (greedy!)
V={1,3} E’= {1,3)
1
10
5
1
8
2
1
3
3
4
6
1
4
6
2
5
Prim’s algorithm
Repeat until all vertices have been
chosen
1
Choose the vertex u not in V such that
edge weight from v to a vertex in V is
minimal (greedy!)
10
5
1
V= {1,3,4} E’= {(1,3),(3,4)}
V={1,3,4,5} E’={(1,3),(3,4),(4,5)}
8
2
3
3
4
….
V={1,3,4,5,2,6}
1
6
1
E’={(1,3),(3,4),(4,5),(5,2),(2,6)}
4
6
2
5
Prim’s algorithm
Repeat until all vertices
have been chosen
1
10
5
1
V={1,3,4,5,2,6}
E’={(1,3),(3,4),(4,5),(5,2),(2,6
)}
8
2
1
3
4
6
1
Final Cost: 1 + 3 + 4 + 1 + 1 =
10
3
4
6
2
5
Prim's Algorithm
This algorithm starts with one node. It then, one
by one, adds a node, that is unconnected to the
new graph to the new graph, each time selecting
the node whose connecting edge has the smallest
weight out of the available nodes’ connecting
edges.
The steps are:
1. The new graph is constructed - with one node from the old
graph.
2. While new graph has fewer than n nodes,
1. Find the node from the old graph with the smallest
connecting edge to the new graph,
2. Add it to the new graph
Every step will have joined one node, so that at the end we will
have one graph with all the nodes and it will be a minimum
spanning tree of the original graph.
Complete Graph
4
B
4
A
C
2
4
E
1
F
2
D
1
3
10
5
G
5
6
3
4
I
H
2
J
3
Old Graph
New Graph
4
B
4
A
C
2
4
E
1
1
F
A
5
G
6
C
2
4
E
3
10
5
4
1
2
D
4
B
F
2
D
1
3
10
5
G
3
5
4
6
3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G
3
5
4
6
3
I
4
H
I
2
J
3
H
2
J
3
Complete Graph
4
B
4
A
Minimum Spanning Tree
C
2
4
E
1
1
F
A
5
G
6
C
2
E
3
10
5
4
1
2
D
4
B
1
F
2
D
G
3
3
4
I
H
I
2
J
3
H
2
J
3
Prim’s
Algorithm
Initialization
a. Pick a vertex r to be the root
b. Set D(r) = 0, parent(r) = null
c. For all vertices v  V, v  r, set D(v) = 
d. Insert all vertices into priority queue P,
using distances as the keys
9
a
2
5
4
c
b
6
d
e a b c d
4
5
5
e
0    
Vertex Parent
e
-
Prim’s Algorithm
While P is not empty:
1. Select the next vertex u to add to the tree
u = P.deleteMin()
2. Update the weight of each vertex w adjacent to
u which is not in the tree (i.e., w  P)
If weight(u,w) < D(w),
a. parent(w) = u
b. D(w) = weight(u,w)
c. Update the priority queue to reflect
new distance for w
Prim’s algorithm
e d b c a
9
a
2
5
6
d
4
4
c
b
0    
Vertex Parent
e
b
c
d
-
5
5
e
d b c a
4 5 5 
Vertex Parent
e
b
e
c
e
d
e
The MST initially consists of the vertex e, and we update
the distances and parent for its adjacent vertices
Prim’s algorithm
d b c a
9
a
2
5
4
c
b
4 5 5 
Vertex Parent
e
b
e
c
e
d
e
6
d
4
5
5
e
a c b
2 4 5
Vertex Parent
e
b
e
c
d
d
e
a
d
Prim’s algorithm
a c b
2 4 5
9
a
2
5
4
c
b
6
d
4
5
Vertex Parent
e
b
e
c
d
d
e
a
d
5
e
c b
4 5
Vertex Parent
e
b
e
c
d
d
e
a
d
Prim’s algorithm
c b
9
a
2
5
4
c
b
4 5
6
d
4
5
Vertex Parent
e
b
e
c
d
d
e
a
d
5
e
b
5
Vertex Parent
e
b
e
c
d
d
e
a
d
Prim’s algorithm
b
9
a
2
5
4
c
b
5
6
d
4
5
Vertex Parent
e
b
e
c
d
d
e
a
d
5
e
The final minimum spanning tree
Vertex Parent
e
b
e
c
d
d
e
a
d
Example
2
7
B
9
8
C
F
8
8
0
A
2
E
5
C
0
9
5
C
F
8
A
2
E
7
4
9
F
8

5
C
3
8
7
0
A
4
9
F
8
3
E
7
7
7
B
5
2
D
7

3
7
E
7
4
8
0
7
B
5
2
7
B
8
A
D
7

D
7
3
7
5
2
2
4
5
2

D
7
4
Example (contd.)
2
7
5
C
8
0
A
7
B
5
2
D
4
9
F
8
3
E
7
4
3
2
B
5
C
5
2
8
0
A
D
7
7
4
9
F
8
3
E
7
3
4
Another
Approach
• Create a forest
of trees from the vertices
• Repeatedly merge trees by adding “safe edges”
until only one tree remains
• A “safe edge” is an edge of minimum weight which
does not create a cycle
9
a
2
5
6
d
4
4
c
b
5
5
e
forest: {a}, {b}, {c}, {d}, {e}
Example
1
10
5
1
8
2
1
1
3
3
4
6
1
4
6
2
5
Initialization
1
Initially, Forest of 6 trees
F= {{1},{2},{3},{4},{5},{6}}
2
3
6
5
Edges in a heap (not
shown)
4
Step 1
1
Select edge with lowest
cost (2,5)
Find(2) = 2, Find (5) =
5
2
Union(2,5)
3
1
F= {{1},{2,5},{3},{4},{6}}
1 edge accepted
6
5
4
Step 2
Select edge with lowest
cost (2,6)
1
Find(2) = 2, Find (6) =
6
2
Union(2,6)
F= {{1},{2,5,6},{3},{4}}
2 edges accepted
3
1
1
6
5
4
Step 3
1
Select edge with lowest
cost (1,3)
1
Find(1) = 1, Find (3) = 3
2
Union(1,3)
F= {{1,3},{2,5,6},{4}}
3 edges accepted
3
1
1
6
5
4
Step 4
1
Select edge with lowest
cost (5,6)
1
Find(5) = 2, Find (6) =
2
2
Do nothing
F= {{1,3},{2,5,6},{4}}
3
1
1
3 edges accepted
6
5
4
Step 5
1
Select edge with lowest
cost (3,4)
1
Find(3) = 1, Find (4) =
4
2
Union(1,4)
F= {{1,3,4},{2,5,6}}
4 edges accepted
3
1
1
6
5
3
4
Step 6
Select edge with lowest cost
(4,5)
1
Find(4) = 1, Find (5) = 2
Union(1,2)
1
F= {{1,3,4,2,5,6}}
2
5 edges accepted : end
Total cost = 10
Although there is a unique
spanning tree in this
example, this is not
generally the case
3
1
4
1
6
3
5
4
Kruskal's Algorithm
This algorithm creates a forest of trees. Initially the forest consists of n single node trees
(and no edges). At each step, we add one edge (the cheapest one) so that it joins two trees
together. If it were to form a cycle, it would simply link two nodes that were already part of
a single connected tree, so that this edge would not be needed.
The steps are:
1. The forest is constructed - with each node in a separate tree.
2. The edges are placed in a priority queue.
3. Until we've added n-1 edges,
1. Extract the cheapest edge from the queue,
2. If it forms a cycle, reject it,
3. Else add it to the forest. Adding it to the forest will join two trees together.
Every step will have joined two trees in the forest together, so that at the end, there
will only be one tree in T.
Complete Graph
4
B
4
A
C
2
4
E
1
F
2
D
1
3
10
5
G
5
6
3
4
I
H
2
J
3
4
B
4
A
C
2
4
E
1
B
A
1
D
B
4
C
B
4
D
B
10
J
C
2
E
C
1
F
D
5
H
D
6
J
E
2
G
F
3
G
F
5
I
G
3
I
G
4
J
H
2
J
I
3
J
1
3
10
5
G
5
4
F
2
D
A
6
3
4
I
H
2
J
3
Sort Edges
(in reality they are placed in a priority
queue - not sorted - but sorting them
makes the algorithm easier to visualize)
4
B
4
A
C
2
4
E
1
D
C
1
F
C
2
E
E
2
G
H
2
J
F
3
G
G
3
I
I
3
J
A
4
B
B
4
D
B
4
C
G
4
J
F
5
I
D
5
H
D
6
J
B
10
J
1
3
10
5
G
5
1
F
2
D
A
6
3
4
I
H
2
J
3
Add Edge
4
B
4
A
C
2
4
E
1
D
C
1
F
C
2
E
E
2
G
H
2
J
F
3
G
G
3
I
I
3
J
A
4
B
B
4
D
B
4
C
G
4
J
F
5
I
D
5
H
D
6
J
B
10
J
1
3
10
5
G
5
1
F
2
D
A
6
3
4
I
H
2
J
3
Add Edge
4
B
4
A
C
2
4
E
1
D
C
1
F
C
2
E
E
2
G
H
2
J
F
3
G
G
3
I
I
3
J
A
4
B
B
4
D
B
4
C
G
4
J
F
5
I
D
5
H
D
6
J
B
10
J
1
3
10
5
G
5
1
F
2
D
A
6
3
4
I
H
2
J
3
Add Edge
4
B
4
A
C
2
4
E
1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J
1
3
10
5
G
5
1
F
2
D
A
6
3
4
I
H
2
J
3
Cycle
A
1
D
C
1
F
C
2
E
E
2
G
H
2
J
F
3
G
G
3
I
I
3
J
A
4
B
B
4
D
B
4
C
G
4
J
F
5
I
D
5
H
D
6
J
B
10
J
Don’t Add Edge
4
B
4
A
C
2
4
E
1
F
2
D
1
3
10
5
G
5
6
3
4
I
H
2
J
3
Add Edge
4
B
4
A
C
2
4
E
1
D
C
1
F
C
2
E
E
2
G
H
2
J
F
3
G
G
3
I
I
3
J
A
4
B
B
4
D
B
4
C
G
4
J
F
5
I
D
5
H
D
6
J
B
10
J
1
3
10
5
G
5
1
F
2
D
A
6
3
4
I
H
2
J
3
Minimum Spanning Tree
Complete Graph
4
B
4
C
2
A
E
1
2
D
4
1
F
4
B
A
C
2
4
E
1
F
2
D
3
10
G
5
G
3
5
6
3
4
I
H
I
H
2
J
1
3
2
J
3
Example
1
10
5
1
8
2
1
1
3
3
4
6
1
4
6
2
5
Initialization
1
Initially, Forest of 6 trees
F= {{1},{2},{3},{4},{5},{6}}
2
3
6
5
Edges in a heap (not
shown)
4
Step 1
1
Select edge with lowest
cost (2,5)
Find(2) = 2, Find (5) =
5
2
Union(2,5)
3
1
F= {{1},{2,5},{3},{4},{6}}
1 edge accepted
6
5
4
Step 2
Select edge with lowest
cost (2,6)
1
Find(2) = 2, Find (6) =
6
2
Union(2,6)
F= {{1},{2,5,6},{3},{4}}
2 edges accepted
3
1
1
6
5
4
Step 3
1
Select edge with lowest
cost (1,3)
1
Find(1) = 1, Find (3) = 3
2
Union(1,3)
F= {{1,3},{2,5,6},{4}}
3 edges accepted
3
1
1
6
5
4
Step 4
1
Select edge with lowest
cost (5,6)
1
Find(5) = 2, Find (6) =
2
2
Do nothing
F= {{1,3},{2,5,6},{4}}
3
1
1
3 edges accepted
6
5
4
Step 5
1
Select edge with lowest
cost (3,4)
1
Find(3) = 1, Find (4) =
4
2
Union(1,4)
F= {{1,3,4},{2,5,6}}
4 edges accepted
3
1
1
6
5
3
4
Step 6
Select edge with lowest cost
(4,5)
1
Find(4) = 1, Find (5) = 2
Union(1,2)
1
F= {{1,3,4,2,5,6}}
2
5 edges accepted : end
Total cost = 10
Although there is a unique
spanning tree in this
example, this is not
generally the case
3
1
4
1
6
3
5
4
Kruskal’s
Initialization algorithm
a. Create a set for each vertex v  V
b. Initialize the set of “safe edges” A comprising the MST
to the empty set
c. Sort edges by increasing weight
9
a
2
5
4
c
b
6
d
4
5
5
e
F = {a}, {b}, {c}, {d}, {e}
A= 
E = {(a,d), (c,d), (d,e), (a,c),
(b,e), (c,e), (b,d), (a,b)}
Kruskal’s algorithm
For each edge (u,v)  E in increasing order while
more than one set remains:
If u and v, belong to different sets U and V
a. add edge (u,v) to the safe edge set
A = A  {(u,v)}
b. merge the sets U and V
F = F - U - V + (U  V)
Return A
Kruskal’s algorithm
9
a
2
5
4
c
b
6
d
E = {(a,d), (c,d), (d,e), (a,c),
4
5
Forest
{a}, {b}, {c}, {d}, {e}
{a,d}, {b}, {c}, {e}
{a,d,c}, {b}, {e}
{a,d,c,e}, {b}
{a,d,c,e,b}
5
(b,e), (c,e), (b,d), (a,b)}
e
A

{(a,d)}
{(a,d), (c,d)}
{(a,d), (c,d), (d,e)}
{(a,d), (c,d), (d,e), (b,e)}
Kruskal
Example
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
2704
BOS
867
849
ORD
740
621
1846
337
LAX
1391
1464
1235
187
144
JFK
1258
184
802
SFO
PVD
BWI
1090
DFW
946
1121
MIA
2342
2704
BOS
867
849
ORD
740
621
1846
337
LAX
1391
1464
1235
187
144
JFK
1258
184
802
SFO
PVD
BWI
1090
DFW
946
1121
MIA
2342
2704
BOS
867
849
ORD
740
621
1846
337
LAX
1391
1464
1235
187
144
JFK
1258
184
802
SFO
PVD
BWI
1090
DFW
946
1121
MIA
2342
2704
Example
849
ORD
740
621
1846
LAX
1391
1464
1235
PVD
187
144
JFK
1258
184
802
SFO
337
BOS
867
BWI
1090
DFW
946
1121
MIA
2342
Minimum Spanning Trees
125
2704
BOS
867
849
ORD
740
621
1846
337
LAX
1391
1464
1235
187
144
JFK
1258
184
802
SFO
PVD
BWI
1090
DFW
946
1121
MIA
2342
2704
Example
849
ORD
740
621
1846
LAX
1391
1464
1235
PVD
187
144
JFK
1258
184
802
SFO
337
BOS
867
BWI
1090
DFW
946
1121
MIA
2342
Minimum Spanning Trees
2704
Example
849
ORD
740
621
1846
LAX
1391
1464
1235
PVD
187
144
JFK
1258
184
802
SFO
337
BOS
867
BWI
1090
DFW
946
1121
MIA
2342
Minimum Spanning Trees
2704
Example
849
ORD
740
621
1846
LAX
1391
1464
1235
PVD
187
144
JFK
1258
184
802
SFO
337
BOS
867
BWI
1090
DFW
946
1121
MIA
2342
Minimum Spanning Trees
2704
Example
849
ORD
740
621
1846
LAX
1391
1464
1235
PVD
187
144
JFK
1258
184
802
SFO
337
BOS
867
BWI
1090
DFW
946
1121
MIA
2342
Minimum Spanning Trees
2704
Example
849
ORD
740
621
1846
LAX
1391
1464
1235
PVD
187
144
JFK
1258
184
802
SFO
337
BOS
867
BWI
1090
DFW
946
1121
MIA
2342
Minimum Spanning Trees
2704
Example
849
ORD
740
621
1846
LAX
1391
1464
1235
PVD
187
144
JFK
1258
184
802
SFO
337
BOS
867
BWI
1090
DFW
946
1121
MIA
2342
Minimum Spanning Trees
2704
BOS
867
849
ORD
740
621
1846
337
LAX
1391
1464
187
144
JFK
1258
184
802
SFO
PVD
BWI
1090
1235
DFW
946
1121
MIA
2342
Minimum Spanning Trees
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
Shortest Path Problem
 Given a weighted graph and two vertices u and v, we want to
find a path of minimum total weight between u and v.
 Length of a path is the sum of the weights of its edges.
 Example:
 Shortest path between X & Y
 Applications
 Internet packet routing
 Flight reservations
 Driving directions
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
Shortest Paths
 In an edge-weighted graph, the weight of an edge
measures the cost of traveling that edge.
 For example, in a graph representing a network of
airports, the weights could represent: distance,
cost or time.
 Such a graph could be used to answer any of the
following:
 What is the fastest way to get from A to B?
 Which route from A to B is the least expensive?
 What is the shortest possible distance from A to
B?
 Each of these questions is an instance of the same
problem:
The shortest path problem!
Variants of Shortest Path
 Single-source shortest paths
 G = (V, E)  find a shortest path from a given source
vertex s to each vertex v  V
 Single-destination shortest paths
 Find a shortest path to a given destination vertex t from
each vertex v
 Reversing the direction of each edge  single-source
Variants of Shortest Paths (cont’d)
 Single-pair shortest path
 Find a shortest path from u to v for given vertices u and v
 All-pairs shortest-paths
 Find a shortest path from u to v for every pair of vertices
u and v
Example
0
0
A
8
4
A
8
2
B
8
7
2
2
C
2
3

1
9
E
D

F
4
5
B
8
7
3
5
2
C
E
B
2
E
8
3
5
4
A
8
4
2
C
3
5
D
F
2
7
1
0
A
8
2
9
0
8
4
2
9
1
11
F
D
5
3
B
2
7
7
C
3
5
E
2
1
9
D
8
F
5
3
Example (cont.)
0
A
8
4
2
B
2
7
7
C
3
5
E
2
1
9
D
8
F
3
5
0
A
8
4
2
B
2
Shortest Paths
7
7
C
3
5
E
2
1
9
D
8
F
5
3
Dijkstra’s Algorithm
 A priority queue stores
the vertices outside the
cloud
 Key: distance
 Element: vertex
 Locator-based methods
 insert(k,e) returns a
locator
 replaceKey(l,k)
changes the key of an
item
 We store two labels with
each vertex:
 Distance (d(v) label)
 locator in priority
queue
Algorithm DijkstraDistances(G, s)
Q  new heap-based priority queue
for all v  G.vertices()
if v = s
setDistance(v, 0)
else
setDistance(v, )
l  Q.insert(getDistance(v), v)
setLocator(v,l)
while Q.isEmpty()
u  Q.removeMin()
for all e  G.incidentEdges(u)
{ relax edge e }
z  G.opposite(u,e)
r  getDistance(u) + weight(e)
if r < getDistance(z)
setDistance(z,r)
Q.replaceKey(getLocator(z),r)
Dijkstra Algorithm (Single Source
Shortest Path Problem Algorithm)
 Dijkstra's algorithm solves the problem of finding the
shortest path from a point in a graph (the source) to a
destination.
 It turns out that one can find the shortest paths from a
given source to all points in a graph in the same time,
hence this problem is sometimes called the singlesource shortest paths problem.
Dijkstra Algorithm
g
E
0a 3a 5a 5b 11c 8c 9f
0a 3a 5a 5b 11c 8c 9f
Warshall’s Algorithm
 Suppose G is a directed graph with n nodes v1, v2, ..., vn
and we want to find the path matrix of graph G. For
this purpose, Warshall gave an algorithm to find the
transitive closure of graph. The n-square boolean
matrix, P0, P1,..., Pn is defined as follows. Suppose Pk
[i] [j] denote the ij entry of the matrix Pk. So
Warshall’s Algorithm (Contd.)
 In other words,
 P0 [i] [j] = 1 If there is an edge between vi to vj
 P1 [i] [j] = 1 If there is a path from vi to vj which does
not use any other nodes except possible v1
 P2 [i] [j] = 1 If there is a path from vi to vj which does
not use any other nodes except possibly v1 and v2
Warshall’s Algorithm (Contd.)
 Warshall stated that Pk [i] [j] = 1 can occur only if one
of the following two cases occurs:
(a) There is a path from vi to vj which does not use any
other nodes except possibly v1, v2,... vk–1, hence
Pk–1 [i] [j] = 1
(b) There is a path form vi to vk and a path from vk to vj
where each path does not use any other nodes except
possibly v1, v2, ..., vk–1, hence
Pk–1 [i] [k] = 1 and
Pk–1 [k] [j] = 1
vi
….
vj
vi
….
vk
… .
vj
Warshall’s Algorithm (Contd.)
 So the elements of matrix Pk can be obtained by
 Pk [i] [j] = Pk–1 [i] [j] V (Pk–1 [i] [k] ᴧ Pk–1 [k] [j])
Warshall’s Algorithm (Contd.)
1. [Initialize P]
Repeat for i, j = 1, 2, ..., n
If A[i] [j] = 0 then set P [i] [j] = 0
else set P[i] [j] = 1
2. Repeat steps 3 to 4 for k = 1 , 2, ..., n
3. Repeat step 4 for i = 1, 2, ..., n
4. Repeat for j = 1, 2, ..., n
Set P [i] [j] = P [i] [j] V (P [i] [k] ᴧ P [k] [j])
5. Exit
Activity Networks
 Activity on Network
 To simplify a project, divide it into several subprojects called




activities.
The successful completion of these activities will result in a
completion of entire project for e.g.,
A student can take DS if he have read programming
previously but some other paper, he can take which does not
depend upon it.
This relationship can be represented by a directed graph in
which the vertices represent courses and the directed edges
represents prerequisitces.
That is, a directed graph G in which the vertices represent
tasks or activities and the edges represent precedence
relations between tasks is an activity on network.
Topological Sorting
Transitive Closure
Download