Upload
leona-casey
View
216
Download
0
Embed Size (px)
Citation preview
WEIGHTED GRAPHS
Weighted Graphs
Graph G = (V,E) such that there are weights/costs associated with each edgew((a,b)): cost of edge (a,b)representation: matrix entries <-> costs
d
ab
c
20
3265
12
e35
Problems on Weighted Graphs
Minimum-cost Spanning TreeGiven a weighted graph G, determine a
spanning tree with minimum total edge cost
Single-source shortest pathsGiven a weighted graph G and a source
vertex v in G, determine the shortest paths from v to all other vertices in G
Path length: sum of all edges in path
Shortest Path
Distance from vertex v to vertex u: d(v,u)- length of the shortest path from v to u, if it exists
Length- sum of the edges’ weights from vertex v to u
Greedy Method (Dijkstra’s Algorithm)
Weighted Breadth First Search1) Starting Vertex v 2) Visit Adjacent vertices 3) Select the vertex whose incident edge has the smallest weight.4) Selected vertex and edge is now part of the “cloud”. (the cloud is the visited vertices and edges).Add the weight of the edge to the running minimum total.5) Evaluate if running minimum time will be shortened by moving to the selected vertex. 5) Move to the selected vertex 6) Repeat step 3 until finished
Djikstra’s Algorithm - example
A
100
DB
EC
9020
75
40
125
Example
From A can go to B (90) and C (100). Will go to B (smaller)
FROM/TO B C D E A 90 100 INF INF
Example
From B can go to D (110:90+20) Will go to C (smaller from A: 100 vs 110 to D)
FROM/TO B C D E A 90 100 INF INF B 90 100 110 INF
Example
From C can go to B (125) and e (175:100+75). Will not change entry in B since previous entry is lower. Will go to D (110 vs 175 to E).
FROM/TO B C D E A 90 100 INF INF B 90 100 110 INF C 90 100 110 175
Example
From D can go to E (150) Willchange entry in E since this path is smaller. Will go to E.
FROM/TO B C D E A 90 100 INF INF B 90 100 110 INF C 90 100 110 175 D 90 100 110 150
Example
From E, all entries remain the same (no smaller values). Algorithm is finished since all vertices already visited
FROM/TO B C D E A 90 100 INF INF B 90 100 110 INF C 90 100 110 175 D 90 100 110 150 E 90 100 110 150
Djikstra’s algorithm
Notice that the algorithm not only gives you the shortest path from A to E, but it also shows you the shortest path to all the other vertices as well (greedy algorithm).
Djikstra’s Algorithm - Query
A
150
D
B
E
C
20
220
30
60
15
26080
F
What is the shortest path from A to F ?
Minimum Spanning Trees (Kruskal’s Algorithm)
Minimum Spanning Problem- Find a tree which contains all vertices with the least total weight
Kruskal’s Algorithm1) Put all edges in a queue2) Select the lowest edge 3) If edge is a subgraph of the cluster, include to the cluster group C, the vertices incident to the edge. Otherwise, merge the vertices to the
current cluster. 4) Repeat until cluster covers all vertices
MST: Kruskal’s Algorithm
Algorithm Kruskal(G)Input: Weighted graph G=(V,E)Output: MCST in G (set of edges)
// InitializeT {}Q priority queue containing all edges e
sorted according to w(e)
Kruskal’s algorithm continued
while not (Q.isempty() or |T| = n-1) do (u,v) Q.remove()
if T + (u,v) contains a cycle then discard (u,v)else add edge (u,v) to T
return T
Prim-Jarnik Algorithm
Similar to Kruskal’s algorithmMain difference is that the edges to
be visited should be incident to the current vertex v(cannot add an edge that is not connected to the current vertex)
select the edge with the lowest weight
Prim’s Algorithm
Algorithm Primm(G)Input: Weighted graph G=(V,E)Output: MCST in G (set of edges)
// InitializeT {}B {s} // s is any vertex in G
Primm’s Algorithm
while |B| < n do(u,v) edge such that
u in B, v not in B, w((u,v)) is minimumadd edge (u,v) to Tadd vertex v to B
return T