• 11-11-2003, 05:31 PM
tempoz
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.