Your SlideShare is downloading. ×
0
Weighted graphs
Weighted graphs
Weighted graphs
Weighted graphs
Weighted graphs
Weighted graphs
Weighted graphs
Weighted graphs
Weighted graphs
Weighted graphs
Weighted graphs
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Weighted graphs

1,311

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,311
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
60
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Weighted graphs <ul><li>Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities. </li></ul><ul><li>CHG </li></ul><ul><li>SF HTD </li></ul><ul><li>OAK </li></ul><ul><li>ATL </li></ul><ul><li>LA </li></ul><ul><li>SD </li></ul><ul><li>V = {SF, OAK, CHG, HTD, ATL, LA, SD} </li></ul><ul><li>E = {{SF, HTD}, {SF, CHG}, {SF, LA}, {SF, SD}, {SD, OAK}, {CHG, LA}, </li></ul><ul><li>{LA, OAK}, {LA, ATL}, {LA, SD}, {ATL, HTD}, {SD, ATL}} </li></ul>         
  • 2. <ul><li>Problem formulation: find the &amp;quot;best&amp;quot; path between two vertices v 1 , v 2  V </li></ul><ul><li>in graph G = (V, E). Depending on what the &amp;quot;best&amp;quot; path means, we have 2 </li></ul><ul><li>types of problems: </li></ul><ul><li>The minimum spanning tree problem , where the &amp;quot;best&amp;quot; path means the &amp;quot;lowest-cost&amp;quot; path. </li></ul><ul><li>The shortest path problem , where the &amp;quot;best&amp;quot; path means the &amp;quot;shortest&amp;quot; path. </li></ul><ul><li>Note that here edge weights are not necessarily Euclidean distances. Example: </li></ul><ul><ul><li>2985 &gt; 1421 + 310, not the case </li></ul></ul><ul><ul><li>here, however. </li></ul></ul>            
  • 3. The Weighted Graph ADT <ul><li>Definition A weighted graph, G, is a triple (V, E, W), where (V, E) is a graph, </li></ul><ul><li>and W is a function from E into Z + , where Z + is a set of all positive integers. </li></ul><ul><li>That is, W : E  Z + . </li></ul><ul><li>Additional operations (methods) on weighted graphs: </li></ul><ul><li>addEdge(v1, v2, weight) Returns G with new edge v1v2 added </li></ul><ul><li>removeEdge(v1, v2, weight) Returns G with edge v1v2 removed </li></ul><ul><li>edgeWeight(v1, v2) Returns the weight of edge v1v2 </li></ul>
  • 4. The minimum spanning tree problem <ul><li>Definition. A minimum spanning tree of a weighted graph is a collection of </li></ul><ul><li>edges connecting all of the vertices such that the sum of the weights of the </li></ul><ul><li>edges is at least as small as the sum of the weights of any other collection of </li></ul><ul><li>edges connecting all of the vertices. </li></ul><ul><li>Example Consider the following graph </li></ul>                       a  b  m  l  k  j  i  h  d  e  f  g  c
  • 5. <ul><li>Property of a minimum spanning tree (MST). Given any division of the </li></ul><ul><li>vertices of a graph into two sets, the minimum spanning tree contains the </li></ul><ul><li>shortest of the edges connecting a vertex in one of the sets to a vertex in the </li></ul><ul><li>other set. </li></ul><ul><li>This property tells us that we can start building the MST by selecting any </li></ul><ul><li>vertex, and always taking next the vertex which is closest to the vertices </li></ul><ul><li>already on the tree. If more than one &amp;quot;closest&amp;quot; vertex exists, then we can take </li></ul><ul><li>anyone of these vertices (therefore, a MST of a graph is not unique). </li></ul><ul><li>Example: Let V1 = {a, b, c, d} , V2 = {e, f, …, m}. Then, the MSP must contain </li></ul><ul><li>edge fd , because W(fd) = 1. </li></ul><ul><li>Note that V2 consists of two types of vertices: </li></ul><ul><li>Fringe vertices, which are adjacent to V1. </li></ul><ul><li>Unseen vertices, which are not adjacent to V1. </li></ul><ul><li>Extended example to be distributed in class! </li></ul>
  • 6. Generation of a MST : the Prim&apos;s algorithm <ul><li>The idea : Select an arbitrary vertex to start the tree. While there are fringe </li></ul><ul><li>vertices remaining, select an edge of minimum weight between a tree vertex </li></ul><ul><li>and a fringe vertex, and add the selected edge and fringe vertex to the tree. </li></ul><ul><li>Algorithm MST (start, T) </li></ul><ul><li>Included[start] = true // Assume Boolean array Included tells, </li></ul><ul><li>for (node = 2) to NumberOfNodes // which vertices are already in the MST. </li></ul><ul><li>Included[node] = false </li></ul><ul><li>for (node = 1) to (NumberOf Nodes - 1) { </li></ul><ul><li>edge = FindMinEdge () // Requires a loop over all of the nodes. </li></ul><ul><li>Included[edge.IncidentNode()] = true </li></ul><ul><li>AddEdge(edge, MST) } </li></ul><ul><li>Efficiency result : Prim&apos;s algorithm for generating a MST is O(N^2), where N is </li></ul><ul><li>the number of nodes in the tree. Since the number of edges is not important it </li></ul><ul><li>is good for dense graphs. </li></ul>
  • 7. Generation of a MST : the Kruskal&apos;s algorithm <ul><li>The idea: Add edges one at a time selecting at each step the shortest edge </li></ul><ul><li>that does not form a cycle. </li></ul><ul><li>Assume that vertices of a MST are initially viewed as one element sets, </li></ul><ul><li>and edges are arranged in a priority queue according to their weights. Then, </li></ul><ul><li>we remove edges from the priority queue in order of increasing weights and </li></ul><ul><li>check if the vertices incident to that edge are already connected. If not, we </li></ul><ul><li>connect them and this way the disconnected components gradually evolve into </li></ul><ul><li>a tree -- the minimum spanning tree. </li></ul><ul><li>Extended example to be distributed in class! </li></ul>
  • 8. <ul><li>Efficiency result: Assume that </li></ul><ul><ul><li>The priority queue is implemented as a heap. </li></ul></ul><ul><ul><li>The minimum spanning tree is implemented as a weight-balanced tree. </li></ul></ul><ul><ul><li>The graph is implemented by means of adjacency lists. </li></ul></ul><ul><ul><li>Then: </li></ul></ul><ul><ul><li>The initial formation of the priority queue of edges is O(NumberOfEdges*log(NumberOfEdges)) operation. </li></ul></ul><ul><ul><li>The phase of removing edges from the queue and performing one or two operations requires also O(NumberOfEdges*log(NumberOfEdges)) time. </li></ul></ul><ul><ul><li>Therefore, the total efficiency of the Kruskal&apos;s algorithm is O(NumberOfEdges*log(NumberOfEdges)). </li></ul></ul>
  • 9. The shortest-path problem <ul><li>Definition. The weight, or length, of a path v 0 , v 1 , v 2 , …, v k in weighted graph </li></ul><ul><li>k-1 </li></ul><ul><li>G = (V, E, W) is  W(v i v i+1 ). Path v 0 , v 1 , v 2 , …, v k is the shortest path from </li></ul><ul><li>i = 0 </li></ul><ul><li>v 0 to v k if there is no other path from v 0 to v k with lower weight. </li></ul><ul><li>Definition. The distance from vertex x to vertex y (x, y  V), denoted as </li></ul><ul><li>d(x,y) is the weight of the shortest path from x to y . </li></ul><ul><li>The problem: Given x  V, we want to find the shortest paths from x to any </li></ul><ul><li>other vertex in V in order of increasing distance from x. Consider the following </li></ul><ul><li>two cases: </li></ul><ul><li>All weights are &amp;quot;1&amp;quot;. Therefore, the problem becomes finding a path containing the minimum number of edges. To solve this problem, we can use the breadth-first search algorithm. </li></ul><ul><li>If edge weights are different, we can use the Dijkstra&apos;s shortest path algorithm. </li></ul>
  • 10. The shortest-path problem: Dijkstra&apos;s algorithm <ul><li>Extended example to be distributed in class! </li></ul><ul><li>To implement Dijkstra&apos;s algorithm we need the following data structures: </li></ul><ul><li>An integer array, distance , of NumberOfNodes size (assuming that edge weights are integers). </li></ul><ul><li>A Node array, path , of NumberOfNodes size. </li></ul><ul><li>A Boolean array, included , of NumberOfNodes size. </li></ul><ul><li>Given the start node, the initialization of these arrays is the following: </li></ul><ul><li>included[start] := true, all other entries in included initialized to false. </li></ul><ul><li>0, if node = start </li></ul><ul><li>distance[node] := EdgeWeight(start, node) </li></ul><ul><li> , if there does not exist a direct edge between </li></ul><ul><li>start and node </li></ul><ul><li>path[node] := start, if there exists an edge between start and node </li></ul><ul><li>undefined, otherwise. </li></ul>
  • 11. Dijkstra&apos;s algorithm (contd.) <ul><li>The iteration phase: </li></ul><ul><li>repeat </li></ul><ul><li>find the node, j, that is at the minimum distance from start among those </li></ul><ul><li>not yet included and make included[j] := true </li></ul><ul><li>for each node, r, not yet included </li></ul><ul><li>if r is connected by an edge to j, then </li></ul><ul><li>if distance[j] + EdgeWeight(j, r) &lt; distance[r] then </li></ul><ul><li>distance[r] := distance[j] + EdgeWeight(j, r) </li></ul><ul><li>path[r] := j // path contains the immediate predecessor of each node </li></ul><ul><li>until included[destination_node] := true </li></ul><ul><li>Efficiency result. If EdgeWeight operation is O(1), then Dijkstra&apos;s algorithm is </li></ul><ul><li>O(NumberOfNodes^2). </li></ul>

×