How would I convert this adjacency matrix into an adjacency list and then print the adjacency list using Java,

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 How would I convert this adjacency matrix into an adjacency list and then print the adjacency list using Java, I have implemented my own graph data structure and have tried to do this conversion in displayAsList() unsuccessfully.

public class Q5Graph
{
int maxsize;
int wmatrix[][];
String labels[];
int vertexCount;

public Q5Graph()
{
   maxsize = 20;
   wmatrix = new int[maxsize][maxsize];
   labels = new String[maxsize];
for(int i=0; i < maxsize; i++)
   for (int j=0; j< maxsize; j++)
{
       wmatrix[i][j] = 0;
}
vertexCount = 0;
}

public void addVertex(String label)
{
if (vertexCount == maxsize)
   {
       throw new IllegalArgumentException("Graph is already full!");
   }
   else if (hasVertex(label))
   {
       throw new IllegalArgumentException("Vertex '"+label+"' already exists");
   }
   else
   {
labels[vertexCount] = label;
vertexCount++;
}
}

public void addEdge(String label1, String label2, int weight)
{
int v1, v2;

   if(!hasVertex(label1)){
throw new IllegalArgumentException("First Vertex '" + label1 + "' does not exist yet!");
}
else if(!hasVertex(label2)){
throw new IllegalArgumentException("Second Vertex '" + label2 + "' does not exist yet!");
}   
   else
   {
       v1 = getIndex(label1);
   v2 = getIndex(label2);
   wmatrix[v1][v2] = weight;
   }
}

public boolean hasVertex(String label)
{
boolean has = false;
for (int i=0; i < vertexCount; i++)
{
if (labels[i].equals(label))
has = true;
}
return has;
}

public int getIndex(String label)
{
int theVertex = -1;
for (int i=0; i < vertexCount; i++)
{
if (labels[i].equals(label))
theVertex = i;
}
   return theVertex;
   }
  
public int[][] getMatrix()
{
   return wmatrix;
}
public String[] getLabels()
{
   return labels;
}
public void displayAsList(int[][] wmatrix, String[] labels) //This is what i have tried
{
   int l = wmatrix[0].length;
   String[] adjList = new String[l];
   //for(int i = 0; i < l; i++)
   //{
   //   adjList[i] = new String[l];  
   //}
   int i, j;
   for(i = 0; i < labels.length; i++)
   {
       for(j = 0; j < wmatrix.length; j++)
       {
           adjList[i] = labels[i];
           if(wmatrix[i][j] != 0)
           {
               adjList[i] = adjList[i]+" -> "+labels[j];
           }
       }
   }
   System.out.println("Adjacency List display (stub)");
   for(int v = 0; v < adjList.length; v++)
   {
       //System.out.print(v);
       for(String u : adjList)
       {
           if(u != null)
           {
               System.out.print(u);
           }
       }
       System.out.println();
   }
}

 
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Concept of memory addresses in pointers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education