Exercise help please!! (Graphs)

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Nov 2003
Posts
1

## Exercise help please!! (Graphs)

Can someone help me doing this exercice, I can do some parts but I don't understand how to do the whole thing.

Driver
File name: L6D.java
1. The driver contains only the main method.
2. Initialize a 2-dimensional array to represent a unweighted graph as an adjacency matrix. (The graph may be directed.) Use 1 to indicate an adjacency, and 0 to indicate no adjacency.
3. Initialize a local variable named numVertices with the number of vertices in the graph.
4. Create an instance of L6 and pass numVertices to the constructor. The instance of L6 contains a method named makeAdjList and a method called displayAdjList.
5. Call the method named makeAdjList. The argument to this method is the array that represents the graph. The method returns nothing.
6. Call the displayAdjList method to display the adjacency list representation of the graph (see Sample Output). The method returns nothing and has no arguments.

Program
File name: L6.java
1. Declare an instance variable array of type Node for the adjacency list.
2. Declare integer instance variables named size, row, and column.
3. Define inner class Node

Inner class Node
Declare instance variables vertexId and next.
inner class constructor
This method has an integer argument that represents the data a node contains. This constructor assigns the argument to the vertexId member of the node and initializes next.

L6 constructor
Calls the usual programmerInfo method. The argument to the constructor is the number of vertices in the graph (passed in from the driver program). This value is assigned to size.

This method is called by the driver program. The argument to makeAdjList is the 2-D adjacency matrix created in main. This method uses the data in the adjacency matrix to construct the adjacency list.
Create an array named adjlist of type Node using the value of size.
Use nested for- loops (row and column are the loop variables) to scan the 2-D adjacency matrix passed in and examine the value in each element. If the value is 1, create a new node, passing the column number to its constructor, and call insert (see below). If the value in the adjacency matrix is 0, examine the next element in the array.

insert method
The arguments to this private method are the row index from the 2-D adjacency matrix and the newly created node. This method inserts the node into the linked list associated with the row in the adjacency matrix.

This method is called from main to display the structure of the adjacency list representation (see sample output). If a given vertex has no adjacent vertices the word NULL is displayed, otherwise vertex identification numbers for the adjacent vertices are displayed in the order in which they occur. Use the display format shown in the sample output.

Sample Output (next page)

1: NULL
2: 1, 4
3: 2, 4
4: 1

2. Senior Member
Join Date
Mar 2003
Posts
834
All you've done is post your coursework specification. As fun as doing your coursework for you is, I don't think you'll find anyone that'll do it for you.

What problem are you having?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source