Samplefinalexamsolution - ECE242 Data Structures and...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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) 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 ArrayLists 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 1 2 1 3 3 3 3 1 1 2 2 2 A[i][j] 0 1 2 3 0 1 1 0 1 1 0 1 1 2 1 1 0 1 3 0 1 1 0 Graph Adjacency List Adjacency Matrix 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 ; } } 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....
View Full Document

This note was uploaded on 02/20/2010 for the course ENGINEERIN 242 211 taught by Professor Tessier during the Spring '10 term at UMass (Amherst).

Page1 / 17

Samplefinalexamsolution - ECE242 Data Structures and...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online