Upload
nora-johnson
View
226
Download
1
Embed Size (px)
Citation preview
1
GRAPHS - ADVANCED APPLICATIONS
Minimim Spanning Trees
Shortest Path
Transitive Closure
2
Graph applicationsGraph applications
Graphs can be used for:
Finding a route to drive from one city to another
Finding connecting flights from one city to another
Determining least-cost highway connections
Designing optimal connections on a computer chip
Implementing compilers
Doing garbage collection
Representing family histories
Doing similarity testing (e.g. for a dating service)
Playing games
3
Elementary Graph OperationsElementary Graph Operations
Graph traversals provide the basis for many elementary graph operations:
Spanning trees on graphs
Graph cycles
Connected components of a graph
4
Spanning Tree
Connected sub graph that includes all vertices of the original connected graph
The subgraph is a tree
If original graph has n vertices, the spanning tree has
n vertices and n-1 edges.
No cycle allowed in this sub graph
5
Minimum-cost spanning tree
A minimum-cost spanning tree of a
connected weighted graph is :
a collection of edges connecting all vertices such that
The sum of the weights of the edges is the smallest possible.
6
Spanning trees Suppose you have a connected undirected graph
ConnectedConnected: every node is reachable from every other node
UndirectedUndirected: edges do not have an associated direction ... then a spanning tree is a connected sub graph in which
there are no cycles
6
A connected,undirected graph
Four of the spanning trees of the graph
7
Finding a spanning tree – We did this in class
To find a spanning tree of a graph,1. pick an initial node and call it part of the spanning
tree 2. 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
7An undirected graph Result of a BFS
starting from topResult of a DFSstarting from top
8
Spanning Tree - Minimum Edges
Minimum number of edges to keep the graph connected
If have N vertices, spanning tree has N-1N-1 edges
9
Minimizing costs
Suppose you want to supply a set of houses (say, in a new subdivision) with:
electric power
Water
sewage lines
telephone lines
9
10
Minimum cost spanning tree
To keep costs down:
Connect these houses with a spanning tree (of, for example, power lines)
However, the houses are not all equal distances apartall equal distances apart
To reduce costs even further, you could connect the
houses with a minimum-cost spanning tree
11
Minimum Spanning Tree (MST)
6
7 1
5
10
20
6 10
1
5
Spanning tree with minimum weight
12
Algorithm For Finding MST
1. All nodes are unselected,
2. Mark node v selected to get started – can be any node
3. For each node of graph,
{
Find the edge with minimum weight minimum weight that connects an unselected node with a selected node
Mark this unselected node as selected
}
13
Demos For Finding MST
Step 1: mark vertex AA as selected
A
B
C
D
21
10
5 6
14
Demos For Finding MST
Step 2: find the minimum weighted edge connected to vertex A, and mark the other vertex on this edge as selected.
We choose {A to D}
A
B
C
D
21
10
5 6
15
Demos For Finding MST
Step 3: find the minimum weighted edge connected to vertices set { A, D } ,
and mark the other vertex on this edge as selected.
We choose { A, B }
A
B
C
D
21
10
5 6
16
Demos For Finding MST
Step 4: find the minimum weighted edge
connected to vertices set
{ A, D, B} , and mark the other vertex on this edge as selected.
We choose {B,C}
vertices set { A, D, B, C} { A, D, B, C}
No more nodes!No more nodes!
A
B
C
D
21
10
5 6
17
Demos For Finding MST
Step 5: All vertices are marked as selected, So we find the minimum spanning tree
A
B
C
D
21
10
5 6
18
Complexity For Finding MST
N nodes, E edges
Analysis: N * E = O(N*E) N * E = O(N*E)
Number of nodes * the number of Edges
Better implementation:
Heap O(logE) where is E is number of edges(weights)
Initialization of heap: O(E log E)O(E log E)
Number of edges * log of E
19
Cost - Definition
Routing Protocol Cost = congestion, bandwidth, delay …
cost Umass
Router
cost
20
Minimum-Cost Spanning Trees
A minimum-cost spanning tree of a connected weighted graph is a collection of edges connecting all vertices collection of edges connecting all vertices such that the sum of the weights of the edges is the smallest possible.
Prim’s algorithmPrim’s algorithm: always pick the edge with the smallest always pick the edge with the smallest weight to weight to any nodeany node. It is a . It is a greedygreedy algorithm. algorithm.
h
2
6
4
2
8
3
7
5
4
a
f
e
b
c
dg
9
h
2
6
4
2
8
3
7
5
4
a
f
e
b
c
dg
9
21
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 }
21
22
MST Algorithms
Finding an edge of lowest cost can be done just by sorting the edges –
so could use a heap
Efficient testing for a cycle requires a fairly complex algorithm (UNION-FIND)
23
MCST --- Another Example
h
2
6
4
2
8
3
7
5
4
a
f
e
b
c
dg
9
h
2
6
4
2
8
3
7
5
4
a
f
e
b
c
dg
9
h
2
6
4
2
8
3
7
5
4
a
f
e
b
c
dg
9
h
2
6
4
2
8
3
7
5
4
a
f
e
b
c
dg
9
24
MCST --- Example (cont’d)
h
2
6
4
2
8
3
7
5
4
a
f
e
b
c
dg
9
h
2
6
4
2
8
3
7
5
4
a
f
e
b
c
dg
9
h
2
6
4
2
8
3
7
5
4
a
f
e
b
c
dg
9
h
2
6
4
2
8
3
7
5
4
a
f
e
b
c
dg
9
All nodes visited!
25
Shortest Path Problem
Weight: cost, distance, travel time, hop …
13
4 10
5
7
2
5
6
u
v
26
Prim’s Algorithm: Heap Implementation
Consider using a heap to hold the keys D[0…n-1].
We use a ( Min Heap):
To find the minimum in heap array using tree formulas, To find the minimum in heap array using tree formulas, we call we call Extract_Min(heap) Extract_Min(heap)
27
ExtractMin
Extract_Min removes the minimum element from the heap
which is in the root
by swapping in the last element. O(1)
Then sift down last element(LgV)
We get O(log V) using the usual removal heap operation
28
PRIMS ALGORITHM- Heap implementation
Extract_Min removes the minimum element from the heap which is in the root.
Heap Implementation:
O(E log V) or
Number of Edges times the log of # of Vertices
29
Single-Source Shortest Paths – Prim’s
To find the shortest path from one given node to all the others
Given a source x. Find the path from x to v
such that v is in V-x and the path is minimum.
If the weights in the edges are all one we already have a solution: breadth first search
30
Single Source Shortest Paths
Given a weighted connected graph G=(V,E), and given a pair of vertices vs (source) and vd (destination) Î V what is the shortest path from vs to vd? That is, what is the path that has the smallest sum of edge weights?
1
2
4
6
3
5
10
7
81
5
67
2
3
5
1
2
4
6
3
5
10
7
81
5
67
2
3
5
Source vertex vs is vertex 1Bold line is shortest path from 1(vs) to 2 (vd)
31
Single Source Shortest Paths
SP from A to H = SP from A to E + SP from E to H.
SP from A to H = SP from A to B + SP from B to H.
SP from A to H = SP from A to C + SP from C to H.
In general: SP from A to H = SP from A to vi + SP from vi to H; vi.
A B E F H 15
A B E G H 14
A C E F H 16
A C E G H 15
A D E F H 26
A D E G H 25
A
B
D
C E
F
G
H
21
6
3
8
17
5 6
5
32
Single Source Shortest Paths
• Use Dijkstra’s (greedy) algorithm if graph has only positive weights
• Use Bellman-Ford’s algorithm if graph has positive and
negative weights (but not negative cycles…)
• Both algorithms can run on directed or undirected graphs
33
Single-Source Shortest Paths The solution when we have different weights is a variation of
Prim's algorithm
Before we chose the node that was closest to the partial minimum tree.
Now we want the node which is closest to the source node
34
Single-Source Shortest Paths
Algorithm:
1.Start from the source node
2.make this node part of the a shortest path tree A=(VA,E
A)
3. Find an edge (x,y) such that x in the VA and y is not,
so that the path from the initial node to y is minimum
4. add y and the edge (x,y) to the tree A.
Algorithm:
1.Start from the source node
2.make this node part of the a shortest path tree A=(VA,E
A)
3. Find an edge (x,y) such that x in the VA and y is not,
so that the path from the initial node to y is minimum
4. add y and the edge (x,y) to the tree A.
35
Single-Source Shortest Paths
The "question" here is: how do we find the next edge to be added in the tree?
We again can make use of existing data structures As the case with Prim's algorithms for the MST we can
use a priority queue
The algorithms described in the following slides is normally attributed to E. Dijkstra
Let's understand the algorithm graphically first.
36
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2 4
2
5
1
6
1
4
5
2
1
2
1
3
1
1
2
37
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2 4
2
5
1
2
6
1
4
53
2
1
2
1
3
1
1
0
A
0
A
38
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2 4
2
5
1
6
14
53
2
1
2
1
3
1
1
2
1
B
1
B
2
F
2
F
6
G
6
G
39
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2 4
2
5
1
2
6
1
4
53
2
1
2
1
3
1
1
CC FF DD EE GG
40
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2
2
5
1
6
1
4
53
2
1
2
1
3
1
1
2
F
2
F
3
D
3
D
6
E
6
E
6
G
6
G
41
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2
2
1
2
6
14
5
3
2
1
2
1
3
1
1
3
D
3
D
4
L
4
L
4
E
4
E
6
G
6
G
42
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2
1
2
6
14
53
2
1
2
1
3
1
1
4
L
4
L
4
E
4
E
6
G
6
G
43
4
E
4
E
5
M
5
M
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2
1
2
6
3
2
1
2
1
3
1
1
6
G
6
G
7
J
7
J
1
44
5
M
5
M
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2
1
1
3
2
1
2
1
3
1
1
5
G
5
G
7
J
7
J
2
45
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2
1
2
1
3
2
1
1
3
1
1
5
G
5
G
7
J
7
J
46
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2
1
2
1
2
1
1
1
1
6
J
6
J
8
H
8
H
47
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2
1
2
1
3
2
1
1
1
1
7
K
7
K
8
H
8
H
1
48
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2
1
2
3
2
1
1
1
1
8
I
8
I
8
H
8
H
3
49
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2
1
2
1
3 1
1
1
1
8
H
8
H
50
An Example(adapted from Sedgewick)
A
F
B
D
C
E
G
H I
J K
L M
1
2
2
2
1
2
1
3
1
1
1
1
51
The Algorithm
The algorithm here is the almost the same as the one for the minimum spanning tree
The distance to the initial node is considered instead of the absolute value of the edge
Inserting the node in the priority queue is done by considering all the edges that leave the node plus the value (the priority) assigned to the node that is being removed.
Delete is done based on the priority queue strategy
52
ANOTHER EXAMPLE
53
Dijkstra’s Algorithm (Cont’d)
A
B
D
C E
F
G
H
21
6
3
8
17
5 6
5
--
A
2
B
1
C
6
D
3
E
?
F
?
G
?
H
A
B
D
C E
F
G
H
21
6
3
8
17
5 6
5
--
A
2
B
1
C
6
D
3
E
10
F
8
G
?
H
S = {A,C,B,E}
S = {A,C,B}
Cost of SP from A to vi through S
54
Dijkstra’s Algorithm (Cont’d)
A
B
D
C E
F
G
H
21
6
3
8
17
5 6
5
--
A
2
B
1
C
6
D
3
E
10
F
8
G
?
H
A
B
D
C E
F
G
H
21
6
3
8
17
5 6
5
--
A
2
B
1
C
6
D
3
E
10
F
8
G
14
H
S = {A,C,B,E,D,G}
S = {A,C,B,E,D}
Cost of SP from A to vi through S
55
Dijkstra’s Algorithm (Cont’d)
A
B
D
C E
F
G
H
21
6
3
8
17
5 6
5
--
A
2
B
1
C
6
D
3
E
10
F
8
G
14
H
A
B
D
C E
F
G
H
21
6
3
8
17
5 6
5
--
A
2
B
1
C
6
D
3
E
10
F
8
G
14
H
S = {A,C,B,E,D,G,F,H}
S = {A,C,B,E,D,G,F}
Cost of SP from A to vi through S
56
Dijkstra’s Algorithm for Shortest Paths
S = {0} /* Current MST */
for i = 0 to n
D[i] = M[0][i] /* Shortest path length from 0 to i */
for i = 1 to n-1
find the smallest D[v] such that v S
S = S {v}
for all vertices u S
if (D[u] > D[v] + M[v][u]) then
D[u] = D[v] + M[v][u]
57
Dijkstra’s Algorithm --- Example
1
2
4
6
3
5
10
7
81
5
67
2
3
5
--
1
3
2
?
3
?
4
?
5
5
6
--
1
3
2
10
3
?
4
?
5
5
6
--
1
3
2
10
3
7
4
12
5
5
6
--
1
3
2
10
3
7
4
12
5
5
6
--
1
3
2
10
3
7
4
11
5
5
6
--
1
3
2
10
3
7
4
11
5
5
6
--
-- 3
-- 3 5
3 7 5
-- 3
58
Example – Dijkstra Algorithm
Greedy Algorithm Assume all weight of edge >0
0
1
2
3
4
5
102 3
1
2
7
49 6
source
node from node V0 to other nodes
V1 10
V2 5
V3
V4
best
59
Animation Online
Dijkstra’s Shortest Path Algorithm - at my website