



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions for assignment 12 of cop 3530, including implementing dijkstra's algorithm to find shortest paths, extending the linkeddigraph class to find simple paths and cycles, and solving the 0-1-2 knapsack problem using a greedy approach.
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Assignment 12 Due: 26th^ Nov 2008
Consider the directed graph G in figure 1.
Use Dijkstra's single-source shortest path algorithm (page 721-726) to calculate the shortest path from vertex 1 to each vertex of the graph shown below. The number along each edge between two vertices is the weight of the edge. Show the predecessor and distance arrays following each step of Dijkstra's algorithm. Refer to Figure 18.9 on page 722.
In this assignment, you will extend the LinkedDigraph class to support several additional functions related to simple paths and simple cycles.
PLEASE put all your solutions in one single java file. Name this file MyLinkedDigraph.java.
a) Suppose you have a directed graph G=(V,E), develop a new method FindPathLen(int i,int j,int k), which will print the simple path between vertex i and j, whose length is k. If there are multiple such paths, just print one of them. For example, if MyLinkedDigraph g equals to:
When g.FindPathLen(2,3,4) is called, the output should be "2->5->4-
1->3". When g.FindPathLen(2,3,3) is called, the output should be "No path exists."
b) Develop a new method FindAllPaths(int i,int j), which will print all simple paths between vertex i and j. For example, for the graph g above, when g.FindAllPaths(4,5) is called, the output should be: 4->1->3->2-> 4->1->3-> 4->1->2->
You are NOT allowed to call any method of the LinkedDigraph class. Calling one of these methods can cost up to 100% of the question grade.
The template is:
package dataStructures;
public class MyLinkedDigraph extends LinkedDigraph {
int nodeNumber_;
a) In the 0-1 Knapsack problem, given n items with weights w(1), ..., w(n) and values v(1),...,v(n) one chooses k items such that all their weights are less than (or equal to) c so that the sum of the values of those items are maximized. The 0-1 part means that you either take the item or you don't, but you cannot take the item twice.
In this problem, we still try to maximize the values of the items which have total weight of less than c, but this time it is possible to take an item twice. But when taking the item the second time, the value will be decreased by half, i.e. if item m is already taken so that it takes w(m) weight and adds v(m) value, the second time we take it it still adds w(m) weight, but the value this time will be v(m) / 2.
Solve this problem using a greedy approach (your solutions need not find the optimal solution), by modifying the GreedyKnapsack class in the code.applications package by adding the method
public static double greedyKnapsack012(double[] p, double[] w, double c, int[] x )
Your solution should have a worst case running time of O(nlogn), where n is the number of different items.
b) Generalize the previous problem, so that each item m can be taken any number of times, but taking it the k th time will only add v( m )/k value to the sum. So the second time we take m, it will add v(m)/2 value, the third time will add v(m)/3 value etc. Implement a greedy solution to this problem as:
public static double greedyKnapsack01N(double[] p, double[] w, double c, int[] x )
Your solution should have a worst case running time of O(nlogn + mn),
where n is the number of different items and m is the number of (not necessarily distinct) items in your solution.
c) What are your worst case running times? Can you improve your worst- case running time for part (b)? If yes, how? If no, why not?
The main method should look like:
public static void main(String [] args) { double [] p = {0, 16, 3, 5, 4, 7}; double [] w = {0, 2, 2, 6, 5, 4}; int [] x = new int [6]; int n = 5; double c = 10; System.out.print("Greedy value is "); //Should print
System.out.println(greedyKnapsack012(p, w, c, x)); System.out.print("x vector is "); for (int i = 1; i <= n; i++) System.out.print(x[i] + " "); //Should print 2 1 0 0 1
x = new int [6]; System.out.print("Greedy value is "); //Should print
System.out.println(greedyKnapsack01N(p, w, c, x)); System.out.print("x vector is "); for (int i = 1; i <= n; i++) System.out.print(x[i] + " "); //Should print 5 0 0 0 0 }
Note: