HOMEWORK 6


  1. Consider the following directed graph, which is given in adjacency list form as described above and where vertices have numerical labels:

    1: 9
    2: 3, 5
    3: 7, 9
    4: 2, 3, 6
    5:
    6: 5
    7: 5
    8: 2, 3, 4
    9: 7
    
    1. Show the topological ordering for the graph above using the iterative method covered in lecture (rather than the recursive method in the readings). Use the 367 convention of choosing vertices in increasing numerical order. In each iteration of the algorithm, show the value of num and the stack contents: st.
    2. List the topological ordering of the vertices obtained.
  2. Note: You can just use csv format and explicitly mention which is the top. For example if your stack contents are the one discussed in Monday's lecture, then show:
    top: 4, 5, 6, 3, 2, 1

  3. Consider the following undirected graph, which is given in adjacency matrix form and where vertices have character labels and edges have non-negative integer weights (no value indicates no edge exists):

        A B C D E F G
      +--------------
    A |   2 8 4     2
    B | 2     1 7
    C | 8     9   3 5
    D | 4 1 9   3
    E |   7   3   1
    F |     3   1   9
    G | 2   5     9
    
    1. List the node(s) that have a degree of 4.
    2. Trace Dijkstra's algorithm starting at node E by completing the table started below where each row represents one iteration of the algorithm.
      Iteration List of Visited Vertexes and their
      shortest distances from start
      Priority Queue's items
      (listed in increasing order)
      0 - 0 E
      1 E 0 1 F, 3 D, 7 B
      2 E 0, F 1  
    3. What is the shortest path from E to A? Give a list of vertices starting at E and ending at A.
    4. What is the shortest path from E to G? Give a list of vertices starting at E and ending at G.

  4. Complete the code of the printPairs method. This method takes in an integer array and a sum as argument and prints all pairs of numbers in the array that form the given sum.

    public static void printPairs(int arr[], int sum) {

    For full credit:
    • Your solution should work based on an unsorted array.
    • You may not modify the input array.
    • Your solution should work with a time-complexity of O(N), where N is the number of integers in the array.
    • You may use any appropriate Java data structure implementation from java.util class. Also you may assume that an efficient implementation exists.
    • Your function output should be of the following form:
      Pair with given sum 16 is:
      (10, 6)
      (26, -10)
      (0, 16)

Note: You may assume that the input array doesn't contain duplicate items and under this assumption you should print a pair only one time.

SUBMISSION