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.
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.
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.
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.
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)
2: 1, 4
3: 2, 4
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL