Data Structures

Document Sample
Data Structures Powered By Docstoc
					                                       Last Updated: 12/30/2011 3:05:24 PM

                              CMPS 2153-Data Structures

                                  Test 2 – Take Home

                                              HONOR CODE

                 I ______________________________ have neither given nor received
                 assistance from anyone.

                 I realize that if my answers come close to resembling that of another
                 student and/or digital source, where this resemblance is based on the
                 judgment of the instructor, I can be given a score of zero.

                        Student Signature:________________________________

                        Student M Number:_______________________________

Turn your completed take home exam by Monday, April 12, 2010. Your completed exam will not have ANY
handwritten portions (except your signature) whatsoever and will not be graded if it does.
                                    Last Updated: 12/30/2011 3:05:24 PM

1. Give the appropriate matrix and list implementations using the above graph.

2. Give a depth and breadth first search starting at ‘A’ and choosing next vertices based on alphabetical
   order. Explain the implementations of each (data structures used, etc.).

3. Suppose you have a directed graph representing all the flights that an airline flies. What algorithm
   might be used to find the best sequence of connections from one city to another? If we just wanted to
   find out if there was a flight from one city to another, would you use the same algorithm? Explain and
   justify why you would or wouldn’t.

4. Which graph representation allows the most efficient determination of the existence of a particular
   edge in a graph? Explain and justify.
                                              Last Updated: 12/30/2011 3:05:24 PM

      5. Write some c++ code that would implement the backend of the following interface, with the resulting
         structure. Our goal here is to create a randomly generated graph with the constraints specified by the
         user. You must use the given definitions below:
                       The following input:
                                                                               Could create the following structure:

         Dynamic Structure Generator

         Enter   max   nodes: 5
         Enter   max   links per node: 4
         Enter   max   data value: 15
         Enter   the   seed for srand():34345

         Press any key to continue...


struct Node{
     int Id;
     int Data;
     vector<int> Edges;

vector <Node*> Graph;

To add a Node to the Graph you can use:
       Node *Temp = new node;
       Temp->Id = 0;
       Temp->Data = rand()%15;

You can access this newly added node by using the following syntax:

For example, to add an edge to vertex 0:

Where 3 is the id of the vertex being connected to Vertex 0
                                       Last Updated: 12/30/2011 3:05:24 PM

6. Using the graph below, design a greedy algorithm that will traverse the graph given a source node S and a sink
    node T, and traverse the graph in a greedy fashion (with largest edge weight being the greedy choice), until one
    of three things happens:
       Your stuck with edge to leave on.
       Your back at your starting location, S.
       You found the sink node T.

                                              0   San Francisco ->[1,45]
                                              1   LA
                                              2   San Diego           ->[1,45]
                                              3   Chicago             ->[7,20]->[0,25]
                                              4   Dallas              ->[1,80]->[2,90]
                                              5   Miami               ->[4,50]
                                              6   New York            ->[3,75]->[7,100]->[4,125]->[5,90]
                                              7   Denver              ->[0,75]->[1,100]

  For example, if the S = New York and T = L.A. you would traverse in this fashion:
                        New York -> Dallas (125 > 75,100,90)
                        Dallas -> San Diego (90 > 80)
                        San Diego -> L.A. (only edge)

  Using the list based implementation shown above, and assuming a graph definition similar to problem 5, write
  your greedy algorithm in C++ like pseudo code.
                                          Last Updated: 12/30/2011 3:05:24 PM

7. Describe and justify Dijkstra’s algorithm for finding the shortest path between two vertices in a directed graph
    with non-negative lengths associated with its edges. In your justification, I want you to give me a weak proof
    (in your own words) of why Dijkstra’s gives the optimal solution.

8. How may Djikstra's algorithm be modified to determine if it is possible to get from a given vertex to
   all other vertices in the graph?

9. A directed graph of n nodes numbered 1, 2, . . . , n can be represented by an n × n adjacency matrix G1, where
    G1[i, j] is true if there is an edge connecting node i to node j, and G1[i, j] is false otherwise.

    By extension, define Gk to be that matrix such that Gk[i, j] is true if there is a path of length <= k connecting
    node i to node j, and Gk[i, j] is false otherwise.

    Describe an algorithm to generate G2 from G1 using C++ like pseudo code.

10. Insert these keys into an AVL Tree: 342, 206, 444, 523, 607, 301, 142, 183, 102, 157, 149, illustrating
    the problematic insertions.

11. Describe the AVL Tree’s insert method and explain why its insertion time complexity is still of the
    same order as a Binary Tree. What are the differences between an AVL Tree and a Binary Tree?
    When would you use an AVL Tree?
                           Last Updated: 12/30/2011 3:05:24 PM

Label the balancing factor for each               Insert 60 into the following tree:
node in the following tree

   Insert 60 into the following tree:              Insert 5 into the following tree:

Shared By: