Assignment Solutions: Dijkstra's Algorithm, Simple Paths, and Knapsack Problem, Assignments of Data Structures and Algorithms

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

Pre 2010

Uploaded on 03/18/2009

koofers-user-lpj
koofers-user-lpj 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COP 3530
Assignment 12
Due: 26
th
Nov 2008
1. Dijkstra's algorithm
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.
2. [Extra Credit - 50% of the Assignment] A path with no repeated
vertices is called a simple path, and a cycle with no repeated vertices aside
from the start/end vertex is a simple cycle.
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:
pf3
pf4
pf5

Partial preview of the text

Download Assignment Solutions: Dijkstra's Algorithm, Simple Paths, and Knapsack Problem and more Assignments Data Structures and Algorithms in PDF only on Docsity!

COP 3530

Assignment 12 Due: 26th^ Nov 2008

  1. Dijkstra's algorithm

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.

  1. [ Extra Credit - 50% of the Assignment ] A path with no repeated vertices is called a simple path, and a cycle with no repeated vertices aside from the start/end vertex is a simple cycle.

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_;

  1. 0-1-2 Knapsack Problem

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:

  1. The reason we are modifying the class rather than extending it is that some necessary parts of the class are made private, so a subclass will not be able to use them.
  2. This class should be in the "applications" package, NOT "dataStructures" package.