hw7_sol - CSE 413 Homework 7 Solutions 1 The general idea...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 413 Homework 7 Solutions 1) The general idea is to follow the standard approach for a shortest path, but to consider vertices instead of edges. A heap to find the next smallest path edge based on edges can then be avoided. Start with the source vertex s. Initialize an array A of size |V| with the current shortest path lengths from the source to all vertices, and a flag to indicate if each path is a final path. Initialize the path lengths to all vertices except for s to and the path length to s to zero. The path to each vertex is not final, including the vertex s. Perform the following operation. Scan the array A and select the vertex v with minimum path length that is not final and is less than . Mark the path to that vertex as final. Consider all the neighbors of v which are reachable in one step from v (in other words, the neighbors with an out edge from v). For each of these neighbors of v, consider the length of the path to v, plus the weight of the edge from v to each of its neighbors. Scan the shortest path array, and for each of these neighbors update the length if the new length is smaller. Continue doing this until all vertices are final. If the actual shortest path is desired, also store the last vertex along the path to each vertex in the array. This allows the path of each to be traced back. There are |V| iterations as each iteration finalizes the path to a single vertex, and continues until the path to all vertices are final, or there are no more selectable vertices (as in a disconnected graph). Each iteration takes a scan in O(|V|) time of the vertex array, a maximum of another scan of the neighbors of the new node, and a final update of the vertex array. Thus, each iteration takes O(|V|) time. This gives O(|V| 2 ) time. Pseudocode double costs[|V|]; boolean final[|V|]; Vertex lastVertices[|V|]; int total_final =0; for(int i=0; i<|V|;i++) { costs[i] = ; final[i] = false; lastVertices[i] = Null; } costs[Start] = 0; while(total_final<|V|) { double min_cost = ;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
for(int i=0; i<|V|;i++) { if(costs[i]<min_cost && cost[i]< && !final[i]) { min_cost = costs[i]; min_index = i; } } if(min_index==-1)//no selectable vertices { break; } final[min_index] = true; total_final++; for(each edge e in adjacency_list[min_index]) { if(costs[min_index] + e.weight < costs[e.destination_vertex]) { costs[e.destination_vertex] = costs[min_index]+e.weight; lastVertices[e.destination_vertex] = min_index; } } } ShortestPath = {}; Node current = path_destination; while(current!=start && current!=null) { shortestPath = current + ShortestPath;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

hw7_sol - CSE 413 Homework 7 Solutions 1 The general idea...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online