



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Programming Assignment INDIVIDUAL WORK ON THIS ASSIGNMENT IS MANDATORY Due Date: May 20, 2009 at 2:00 PM Remember: Points will be deducted for homework turned in after the Due Date. Deadline: May 26, 2009 at 2:00 PM Remember: Projects will not be accepted after the Deadline VERSION 2.2 (changes are underlined)
For this assignment you will code in C++ several algorithms to provide different solutions to the Shortest Path multicast (sp-multicast) Tree problem. Given an edge weighted undirected graph G = (V, E), a source node s ∈ V , and a subset of vertices T ⊆ V − {s}, the problem is to find a tree that is simply a subgraph of G such that there is a path (in the tree) from node s to every node x ∈ T and the sum of the weight of the edges in the tree is least possible. Furthermore, for every node x ∈ T the weight of the path from node s to node x in the tree should be equal to the weight of a shortest path from node s to node x in the graph G. The weight of each edge in the graph is a positive integer. This problem arises in many application areas (VLSI, networking (multicasting), parallel computing, etc.), but for brevity we will not elaborate on these applications. The purpose of this assignment is to see how greedy and divide-and-conquer methods algorithms can be used to provide suboptimal solutions to this problem. This problem is an NP-complete (actually NP-hard). Our algorithms will only generate suboptimal solutions. You will need to use the POLKA ANIMATOR package called “SAMBA” to visualize the output generated by your algorithm. As you will see it is simple to use this package, i.e., it is easy to do the “SAMBA”. Additional information will be provided as part of the input when you program is directed to visualize the algorithm. This information will give the position of each vertex in 2D space. An edge between a pair of vertices is the line that joins the two points (in 2D) representing the two vertices. The idea is for you to practice C++ and learn more about it, rather than to make you an expert in C++. Also, the project will bring up details that one has to consider when implementing the algorithms discussed in class. As input we are given an edge weighted undirected graph G = (V, E), where the weight of every edge is a positive integer. You may assume that the graph is connected. We are also given a source node s ∈ V and a subset of vertices T ⊆ V − {s}. When an animation is requested your program will receive additional information. The additional information provided for each vertex i is its location in 2D denoted by (xi, yi), where xi and yi are positive integers. There will NOT be more than one vertex located at the same location in 2D. The weight of an edge joining points (xi, yi) and (xj , yj) is given by the weight w(i, j) and it is NOT necessarily proportional to the distance between the two points it joins. The sp-multicast tree problem is to find a tree of least total edge weight (least sum of the weights of the edges in it), and there is a path in the tree from vertex s to every vertex in T. Furthermore, for every vertex x ∈ T the weight of the path from s to x in the tree is exactly equal to the weight of a shortest path from s to x in the graph G. When T consists of just one vertex, the problem is to find a shortest path from s to the vertex
in T. This case can be solved efficiently (as we have discussed in class). However when there are n/2 vertices in T the problem is computationally intractable, i.e., it is NP-complete. All known algorithms that solve this problem take exponential time. That is why we will develop efficient algorithms that do not guarantee an optimal solution, but generate reasonably good solutions all the time. Our problem is to accept as input an edge weighted graph, the vertex s, the set of vertices T , an embedding of the vertices in 2D (when an animation is requested), and some input parame- ters. Then you just apply the appropriate algorithm to the graph and print out some relevant information. In the next section we explain the different algorithms that you must implement. Then we give the format for the input, explain the animation you must perform, the internal representation, explain the required output, talk about coding and experimentation, and point out the turnin procedure for the project.
In this section we explain the greedy and divide-and-conquer methods, you will be using to solve the sp-multicast Tree problem. We use n to represent the number of vertices in the graph, m is the number of edges in the graph, and t is the number of vertices in T (which is greater than or equal to one and is at most n − 1). We assume that the input graph is connected. We assume that the vertices are labeled { 1 , 2 ,... , n}. Let P = (i 1 , i 2 ,... ik) and Q = (j 1 , j 2 ,... jl) be two paths. Path P starts at vertex i 1 and ends at vertex ik, and path Q starts at vertex j 1 and ends at vertex jl. Define jl+1 = 0 and ik+1 = 0. We say that path P is lexicographically smaller than path Q if there is an integer c such that 0 ≤ c ≤ k, and iz = jz for 1 ≤ z ≤ c and ic+1 < jc+1. For example: (1,5,9) is smaller than (1,6,3,7); (1,3,5) is smaller than (4,1); and (1,3,5) is smaller than (1,3,7). The weight of path Pi is the sum of the weight of the edges in the path. Let T 1 and T 2 be two (disjoint) subset of vertices of graph G. Let P be a path with weight p from a vertex in T 1 to a vertex in T 2. Path P is a shortest path from a vertex in T 1 to a vertex in T 2 if every path from a vertex in T 1 to a vertex in T 2 has weight at least p. Path P is said to be a shortest l-path from a vertex in T 1 to a vertex in T 2 if P is shortest path from a vertex in T 1 to a vertex in T 2 , and path P is smaller in lexicographic order than all other shortest paths from a vertex in T 1 to a vertex in T 2.
In this subsection we discuss two greedy methods.
2.1.1 Shortest paths to a tree
Global G, V, E, T, n, m, t, s Shortest paths to a tree(); // We construct the tree G’ consisting of the set of vertices V ′^ and edges E′ E′^ ← ∅; V ′^ ← {s}; While T is not a subset of V ′^ do
If the number of vertices in T is 1 then { return the tree consisting of the shortest l-path from the only vertex in T to vertex s } While T is not empty do For every vertex i adjacent to vertex s in G do Pi = {x|x ∈ T and there is a shortest path from x to s that visits vertex i} EndFor Let Pj be the set of vertices among all the Pi vertices with the largest number of elements // In case there are more than one of these sets with the largest number of elements select // the set among these sets with the least index (value of j) If Pj − {j} is not an empty set then { W ′^ =Greedy-Divide-and-Conquer(j, Pj − {j}) W is augmented by the edge {s, j} and the tree W ′; } else W is augmented by the edge {s, j} T = T − Pj EndWhile Return Tree W ; end Greedy-Divide-and-Conquer
This will be your own method. Your procedure must take “about” the same time than the slowest of the above three procedures, when those procedures are implemented efficiently.
The first input line contains six positive integer values whose meaning is:
1: use Shortest paths to a tree 2: use Aggregation 3: use Greedy-Divide-and-Conquer 4: use your own method
The next m input lines defines the edges. Each line contains two integers (separated by a blank), denoting the vertices it joins, followed by an integer that specifies the weight of the edge. If the output − f ormat is 3, then there will be n lines that specify the location on the screen of each vertex. Each of these lines has two positive integer numbers. The first one specifies the x-coordinate value and the second one specifies y-coordinate value of the corresponding vertex. Then there are t lines, each line specifies a vertex in T. You may assume that the input is correct, but to play it safe you may want to check that it is a valid input. Note that all your structures must by allocated “DYNAMICALLY” so that the algorithm would work no matter what the input size n and m are provided there is enough space. Read the input with cin (standard input). A sample input file will be added to the Web page in a couple of days.
To see the output generated by your program we will use the SAMBA ANIMATOR. Later on we will give instructions on how to do this.
You may use any of the code in either of the text books. The overall requirement is that all your structures must by allocated “DYNAMICALLY” so that the algorithm works no matter what the input size is provided there is enough space. Read the input with cin. Also, use the “standard output” for writing your output and “standard input” for reading files. When we test your program the output will be piped to samba only when the input file has “output-format=3”. I.e., we will run your program as “executeit < input.1 > output.1” and if “output-format=3” in input.1 then we will run “cat output.1 | /cs/faculty/teo/anim.ok/linux/polka/Samba/samba”.
Your program should not print other lines than the ones specified below. When “output-format is 1” you should print the weight of the sp-multicast tree generated. Then all the edges (in lexicographic order and one per line) in the sp-multicasting tree generated by the program should be printed. For example if the edges are { 1 , 4 }, { 3 , 2 },{ 2 , 1 },{ 5 , 1 },{ 7 , 6 },and { 7 , 1 }, then the edges will be printed as follows
1 2 1 4 1 5 1 7 2 3 6 7
Read the map.html page for cs130b for instructions on how to turnin electronically your code.