{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Samplefinalexamsolution - ECE242 Data Structures and...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
ECE242 Data Structures and Algorithms Fall 2008 Final Examination (120 Minutes, closed book) Name: ___SOLUTION_SET_______ Student ID: ______________________ NOTE: Any questions on writing code must be answered in Java using Data Structures topics covered in the lectures. Question Score 1 (20) 2 (20) 3 (08) 4 (11) 5 (15) 6 (11) 7 (05) 8 (10)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
1. [20 pts] The goal of this question is to write a code to convert a given adjacency list to an adjacency matrix for a graph as illustrated in the following figure. Consider an undirected graph consisting of N vertices that are named 0, 1, 2,…, (N-1) a) [7 pts] Create a class, AdjList . Write a method called populateAdjList() that reads in information from the keyboard using Scanner for each vertex and the vertices which are linked to the vertex. The information appears in the following input format vertex neighbor1 neighbor2 … (e.g. “0 1 2”, for vertex 0). One line is input per vertex. Before the vertex information is input, the user indicates the total number of vertices. This information is stored in an adjacency list. Hint: You could use an array of ArrayList’s to store the adjacency list. public class AdjList { private final int DEFAULT_CAPACITY = 20; Vertex adj_list []; AdjList(){ // Array of objects storing the list of vertices linked to the vertex adj_list = new Vertex[ DEFAULT_CAPACITY ]; } AdjList( int size){ 1 2 0 0 1 2 1 3 3 3 3 1 1 2 2 2 0 0 A[i][j] 0 1 2 3 0 0 1 1 0 1 1 0 1 1 2 1 1 0 1 3 0 1 1 0 Graph Adjacency List Adjacency Matrix
Image of page 2
adj_list = new Vertex[size]; } /* * Taking user inputs fill out the adjacency list structure */ public void populateAdjList(){ int i=0; Scanner scan = new Scanner(System. in ); for (i=0; i< adj_list . length ; i++){ adj_list [i]= new Vertex(); System. out .println( "Enter the number of vertices linked to vertex" +i); int count = scan.nextInt(); System. out .println( "Enter these vertices one on a line..." ); for ( int j=0; j<count; j++){ int temp = scan.nextInt(); adj_list [i].add(temp); } } } //populateAdjList public void viewAdjList(){ for ( int i=0; i< adj_list . length ; i++){ System. out .println( "Elements linked to vertex" +i+ ":" ); for ( int j=0; j< adj_list [i].size(); j++) System. out .println( adj_list [i].get(j)); } } public Vertex[] getAdjList(){ return adj_list ; } }
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
b) [10 pts] Create a class, List2MatrixC , and write a method, list2matrix, that takes adj_list (created in part a ) as one of the inputs and returns a 2D array called adj_matrix that stores the adjacency matrix information for the graph described by adj_list . Also print out the information in the adjacency matrix at the end of list2matrix method.
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern