1 / 19

WEIGHTED GRAPHS

WEIGHTED GRAPHS. Weighted Graphs. Graph 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. a. 20. 35. e. b. 12. 32. c. 65. d. Problems on Weighted Graphs. Minimum-cost Spanning Tree

nortonc
Télécharger la présentation

WEIGHTED GRAPHS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WEIGHTED GRAPHS

  2. Weighted Graphs • Graph 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 a 20 35 e b 12 32 c 65 d

  3. Problems on Weighted Graphs • Minimum-cost Spanning Tree • Given a weighted graph G, determine a spanning tree with minimum total edge cost • Single-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 • 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

  5. 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

  6. Djikstra’s Algorithm - example 75 C 100 E 125 A 40 D B 90 20

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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).

  13. Djikstra’s Algorithm - Query C 30 150 E 15 220 A 60 F B 20 D 80 260 What is the shortest path from A to F ?

  14. 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

  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 • Similar to Kruskal’s algorithm • Main 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

  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

More Related