Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Adjacency list graph

I'm trying to read a text file of a graph and print information about the graph including the order and size of the graph, rather it is a directed or undirected graph, if it is directed the in and out degree, and the and a list of all vertices for which it is adjacent. The problem is the adjacency list is just printing out the list of vertices instead of the adjacency list. Is there something in my code that is problematic? Also I'm unsure of how to go about the in and out degree do you guys have any ideas?

"graphs.txt"
6,1
0,2 0,4 1,4 1,5 2,1 2,3 2,5 3,2 3,4 4,1 4,5 5,1 5,3 5,2

```import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

private Node first;        // beginning of list
private int N;             // size of list

// helper linked list data type
private static class Node {
String name;
Node next;
Node(String name, Node next) {
this.name = name;
this.next = next;
}
}

public boolean isEmpty() { return (first == null); }
public int size()        { return N; }

public void insert(String s) {
first = new Node(s, first);
N++;
}

// return string representation of list (in reverse order of list)
public String toString() {
String s = "";
for (Node x = first; x != null; x = x.next)
s = x.name + "-> " + s;
return "{ " + s+"->" + "}";
}

// return array of strings comprising this adjacency list
public String[] toArray() {
String[] names = new String[N];
int i = N;
for (Node x = first; x != null; x = x.next)
names[--i] = x.name;
System.out.println(names);
return names;
}

public static void main(String[] args) throws FileNotFoundException {

Scanner sc = new Scanner(new File("graphs.txt"));

String line = sc.nextLine();
String[] data=line.split("[\\,]");
int order=Integer.parseInt(data);

int typeOfGraph=Integer.parseInt(data);
//Prints results of the order
System.out.println("The order of the graph = " + order);
while(sc.hasNext())
{
line=sc.nextLine();
int index =0;
int count=0;
char edges = ',';
while(index<line.length()) {

if(line.charAt(index)==edges){
count++;
}
index++;
}
/* PRINTS the result for each line!!! */
System.out.println("The size of graph = "+count);

//Determines if graph is zero or one

String[] data2=line.split("[\\|]");
String[][] vertices = new String[ data2.length ][];
for (int i = 0; i < data2.length; i++){
vertices[i] = data2[i].split("[ ]");
}

count=0;
index=0;
System.out.println("Graph: ");
int i=0;
int j=0;

for ( j = 0; j < vertices.length; j++){
for ( i = 0; i < vertices[j].length; i++){

System.out.print("Index "+i+": "+vertices[j][i]+" \n");

}
}
if(typeOfGraph==1)
{
System.out.println("Graph is a directed graph");

}
if(typeOfGraph==0)
{
System.out.println("Graph is a undirected graph");
}
}
}
}```  Reply With Quote

3. ## Re: Adjacency list graph

Can you explain the difference and show an example?  Reply With Quote

4. ## Re: Adjacency list graph

Yes here's the output:

The order of the graph = 6
The size of graph = 14
Graph:
Index 0: 0,2
Index 1: 0,4
Index 2: 1,4
Index 3: 1,5
Index 4: 2,1
Index 5: 2,3
Index 6: 2,5
Index 7: 3,2
Index 8: 3,4
Index 9: 4,1
Index 10: 4,5
Index 11: 5,1
Index 12: 5,3
Index 13: 5,2
Graph is a directed graph  Reply With Quote
5. ## Re: Adjacency list graph  Reply With Quote