19
WEIGHTED GRAPHS

WEIGHTED GRAPHS. 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:

Embed Size (px)

Citation preview

Page 1: WEIGHTED GRAPHS. 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:

WEIGHTED GRAPHS

Page 2: WEIGHTED GRAPHS. 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:

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

Page 3: WEIGHTED GRAPHS. 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:

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

Page 4: WEIGHTED GRAPHS. 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:

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

Page 5: WEIGHTED GRAPHS. 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:

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

Page 6: WEIGHTED GRAPHS. 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:

Djikstra’s Algorithm - example

A

100

DB

EC

9020

75

40

125

Page 7: WEIGHTED GRAPHS. 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:

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

Page 8: WEIGHTED GRAPHS. 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:

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

Page 9: WEIGHTED GRAPHS. 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:

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

Page 10: WEIGHTED GRAPHS. 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:

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

Page 11: WEIGHTED GRAPHS. 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:

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

Page 12: WEIGHTED GRAPHS. 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:

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

Page 13: WEIGHTED GRAPHS. 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:

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 ?

Page 14: WEIGHTED GRAPHS. 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:

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

Page 15: WEIGHTED GRAPHS. 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:

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)

Page 16: WEIGHTED GRAPHS. 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:

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

Page 17: WEIGHTED GRAPHS. 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:

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

Page 18: WEIGHTED GRAPHS. 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:

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

Page 19: WEIGHTED GRAPHS. 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:

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