Docsity

Practice Problems for Quiz 2 - Algorithms and Data Structures | CSE 331, Quizzes of Computer Science

Toggle sidebar
bg1
CSE 331, Summer 2002, Lecture #14 (6/17/02)
1. Quiz
2. * (Single-Source Shortest) Path Problem (Definition)
(a) Given as input a weighted graph, G= (V, E) and a distinguished (source/root) vertex, s, find the
shortest weighted path from sto every other vertex in G.
3. * SS Shortest Path Problem (sans cost: BFS-Based Algorithm)
(a) Recall the BFS algorithm – each “level” in the graph away from the source vertex receives one
unit’s greater distance than the previous. (The distance[] array in the pseudo-code above stores
this value.)
(b) If all edges have unit cost, there can be no shorter path to any given vertex than that which BFS
finds first (if there were, we would have found/marked it in a previous iteration).
(c) Thus BFS itself provides a shortest path algorithm for non-edge weighted graphs in O(|V|+|E|).
4. SS Shortest Path Problem (Dijkstra’s Algorithm)
(a) Similar to Prim’s algorithm: it builds a subgraph of the given graph starting from the given
vertex, by forming a set of vertices connected by edges of least weight.
(b) 1 edge-evaluate( vertex u, vertex v, weight w(), priority_queue q )
2 { // Given two vertices and a weight function
3 // If v’s current dist. > dist. of u + w(u,v)
4 if ( distance[v] > distance[u] + w(u,v) )
5 {
6 distance[v] = distance[u] + w(u,v); // Use the path w/edge (u,v) instead
7 if ( v is in q ) // Test can be done w/ array: O(1)
8 q.decrease_key( v, distance[v] ); // If v is in q, percolate up
9 predecessor[v] = u; // Change the predecessor
10 }
11 }
12 dijkstra( graph g, weight w(), vertex s )
13 {
14 foreach vertex v ( g.vertices() ) // Initialization begin
15 {
16 distance[v] = infinity;
17 predecessor[v] = NULL;
18 }
19 distance[s] = 0;
20 set s = NULL; // Initialization end
21 priority_queue q = g.vertices(); // Keyed on distance[] metric
22 while ( ! q.empty() ) // While there are still vertices
23 {
24 u = q.delete_min(); // Grab the lowest
25 s = s union { u }; // Add it to our set
26 foreach vertex v ( adjacent to u ) // For all adjacent to new vertex
27 edge-evaluate( u, v, w, q ) // Reevaluate their distance from s
28 }
29 }
(c) Running time: initialization requires O(|V|); building the priority queue requires O(|V|log |V|)
operations; the main loop executes O(|V|) times, the inner loop seeks each edge adjacent to the
vertex set by the outer loop and therefore runs a total of O(|E|) times (or average of O(|E|/|V|)
pf3

Partial preview of the text

Download Practice Problems for Quiz 2 - Algorithms and Data Structures | CSE 331 and more Quizzes Computer Science in PDF only on Docsity!

CSE 331, Summer 2002, Lecture #14 (6/17/02)

  1. Quiz
    • (Single-Source Shortest) Path Problem (Definition)

(a) Given as input a weighted graph, G = (V, E) and a distinguished (source/root) vertex, s, find the shortest weighted path from s to every other vertex in G.

    • SS Shortest Path Problem (sans cost: BFS-Based Algorithm)

(a) Recall the BFS algorithm – each “level” in the graph away from the source vertex receives one unit’s greater distance than the previous. (The distance[] array in the pseudo-code above stores this value.) (b) If all edges have unit cost, there can be no shorter path to any given vertex than that which BFS finds first (if there were, we would have found/marked it in a previous iteration). (c) Thus BFS itself provides a shortest path algorithm for non-edge weighted graphs in O(|V | + |E|).

  1. SS Shortest Path Problem (Dijkstra’s Algorithm)

(a) Similar to Prim’s algorithm: it builds a subgraph of the given graph starting from the given vertex, by forming a set of vertices connected by edges of least weight. (b) 1 edge-evaluate( vertex u, vertex v, weight w(), priority_queue q ) 2 { // Given two vertices and a weight function 3 // If v’s current dist. > dist. of u + w(u,v) 4 if ( distance[v] > distance[u] + w(u,v) ) 5 { 6 distance[v] = distance[u] + w(u,v); // Use the path w/edge (u,v) instead 7 if ( v is in q ) // Test can be done w/ array: O(1) 8 q.decrease_key( v, distance[v] ); // If v is in q, percolate up 9 predecessor[v] = u; // Change the predecessor 10 } 11 } 12 dijkstra( graph g, weight w(), vertex s ) 13 { 14 foreach vertex v ( g.vertices() ) // Initialization begin 15 { 16 distance[v] = infinity; 17 predecessor[v] = NULL; 18 } 19 distance[s] = 0; 20 set s = NULL; // Initialization end 21 priority_queue q = g.vertices(); // Keyed on distance[] metric 22 while (! q.empty() ) // While there are still vertices 23 { 24 u = q.delete_min(); // Grab the lowest 25 s = s union { u }; // Add it to our set 26 foreach vertex v ( adjacent to u ) // For all adjacent to new vertex 27 edge-evaluate( u, v, w, q ) // Reevaluate their distance from s 28 } 29 }

(c) Running time: initialization requires O(|V |); building the priority queue requires O(|V | log |V |) operations; the main loop executes O(|V |) times, the inner loop seeks each edge adjacent to the vertex set by the outer loop and therefore runs a total of O(|E|) times (or average of O(|E|/|V |)

for each iteration); edge-evaluate() runs O(log |V |) time. Therefore adding these together, we arrive at a total of O(|V | + |V | log |V | + |V |(|E|/|V |(log |V |))) = O(|E| log |V |). Improvement to O(|E| + |V | log |V |) is possible using more complex data structures (Fibonacci heaps). (d) What about negative edge weights? What happens? (e) What about negative weight cycles?

  1. (Graph-Theoretic) Networks

(a) A network in graph-theoretic terms is quite different in model than the networks we use daily to exchange data. A (graph-theoretic) network is defined as a directed graph with two specially marked vertices – a “source” s and a “sink” t. A source should have in-degree zero; a sink should have outdegree zero. (b) A flow network adds a capacity function, similar to the weighting function of weighted graphs, that maps from edges to edge capacities and the restriction that at any vertex v that is not s or t, the total incoming flow must be equal to the total outgoing flow. (c) Uses: flow networks can be used to model (oil, water, gas, etc.) pipeline networks, rail networks, QoS-guaranteeing communications networks, etc.

  1. Maximum Network Flow Problem

(a) Problem: given a flow network N (V, E) with source s and sink t and its capacity function c(e), determine the maximum amount of flow (units) that can pass through the network. (b) A given solution is obviously (?) not necessarily unique; most common method for solving the problem is known as the Ford-Fulkerson method. (c) Proof of method correctness is somewhat complex; method itself is surprisingly simple. (d) Solution uses two graphs: Nf and Nr – a flow network and a “residual” graph, respectively. Initially Nf contains only vertices – has zero flow (trivially satisfies flow condition); Nr is a copy of N – a graph with flow capacities written in. (e) The algorithm essentially seeks the maximum-flow path from s to t in the residual graph and then adds those edges from said path to Nf. It then “subtracts” or “reverses” those edges away in the residual graph, providing itself a way to “backtrack” away from a previous choice. (f) Example. (g) Basically, the “backtracking” allows us to find a new subpath from s to t – one that uses part of a path already taken. We then “push back” flow that we’ve found to the point of path divergence and add it back in from the point of divergence to t.

  1. Greedy Algorithms

(a) Studied Examples: Prim, Kruskal, Dijkstra. (b) Greedy algorithms work in phases: at each phase a decision is made that appears to be good, without regard for future consequences. (c) More generally: some local optimum, or “best you can do among given choices”, is chosen; upon algorithm termination, we hope (can prove, if our algorithm is indeed correct) that local optimum always produces optimal subsolutions. (d) Prim & Kruskal: light edge across cut. Dijkstra: cut (vertex subset) path optimality. (e) Examples: change, simple scheduling, Huffman codes

  1. Greedy Example: Giving change

(a) Money Problem: given a positive integer n (a dollar amount), give the smallest number of bills that sum to the integer n using denominations of $20, $10, $5 and $1. (b) Example: n = 49 is 7 with denominations: $20, $20, $5, $1, $1, $1, $