Download presentation
Presentation is loading. Please wait.
Published byLeona Casey Modified over 9 years ago
1
WEIGHTED GRAPHS
2
Weighted Graphs zGraph G = (V,E) such that there are weights/costs associated with each edge Õw((a,b)): cost of edge (a,b) Õrepresentation: matrix entries costs d a b c 20 32 65 12 e 35
3
Problems on Weighted Graphs zMinimum-cost Spanning Tree ÕGiven a weighted graph G, determine a spanning tree with minimum total edge cost zSingle-source shortest paths ÕGiven 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
4
Shortest Path zDistance from vertex v to vertex u: d(v,u) - length of the shortest path from v to u, if it exists zLength - sum of the edges’ weights from vertex v to u
5
Greedy Method (Dijkstra’s Algorithm) zWeighted Breadth First Search 1) 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
6
Djikstra’s Algorithm - example A 100 D B E C 90 20 75 40 125
7
Example zFrom A can go to B (90) and C (100). Will go to B (smaller) z FROM/TO B C D E A 90 100 INF INF
8
Example zFrom B can go to D (110:90+20) Will go to C (smaller from A: 100 vs 110 to D) z FROM/TO B C D E A 90 100 INF INF B 90 100 110 INF
9
Example zFrom 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). z FROM/TO B C D E A 90 100 INF INF B 90 100 110 INF C 90 100 110 175
10
Example zFrom D can go to E (150) Willchange entry in E since this path is smaller. Will go to E. z 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
11
Example zFrom E, all entries remain the same (no smaller values). Algorithm is finished since all vertices already visited z 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 z
12
Djikstra’s algorithm zNotice 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).
13
Djikstra’s Algorithm - Query A 150 D B E C 20 220 30 60 15 260 80 F What is the shortest path from A to F ?
14
Minimum Spanning Trees (Kruskal’s Algorithm) zMinimum Spanning Problem - Find a tree which contains all vertices with the least total weight zKruskal’s Algorithm 1) Put all edges in a queue 2) 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
15
MST: Kruskal’s Algorithm Algorithm Kruskal(G) Input: Weighted graph G=(V,E) Output: MCST in G (set of edges) // Initialize T {} Q priority queue containing all edges e sorted according to w(e)
16
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
17
Prim-Jarnik Algorithm zSimilar to Kruskal’s algorithm zMain 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) zselect the edge with the lowest weight
18
Prim’s Algorithm Algorithm Primm(G) Input: Weighted graph G=(V,E) Output: MCST in G (set of edges) // Initialize T {} B {s} // s is any vertex in G
19
Primm’s Algorithm while |B| < n do (u,v) edge such that u in B, v not in B, w((u,v)) is minimum add edge (u,v) to T add vertex v to B return T
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.