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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Adjacency list graph

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;
     
    public class AdjList {
        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; }
     
        // add s to the adjacency list
        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 {
     
            AdjList adjlist = new AdjList();
            Scanner sc = new Scanner(new File("graphs.txt"));
     
     
     
     
            String line = sc.nextLine();
    	    String[] data=line.split("[\\,]");
    	    int order=Integer.parseInt(data[0]);
     
    	    int typeOfGraph=Integer.parseInt(data[1]);
    	  //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");
    	        }
    	System.out.println("Adjacency List");
            adjlist.insert(line);
            System.out.println(adjlist);
        }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adjacency list graph

    list of vertices instead of the adjacency list.
    Can you explain the difference and show an example?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Adjacency List
    { 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-> ->}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adjacency list graph

    OK, explain what is wrong with it and post what it should look like.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: October 5th, 2013, 09:14 AM
  2. Logic to find adjacency of numbers 0 & 1 in an array
    By kasun.tawlatta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2013, 09:38 PM
  3. Java Bar graph takes user input to create graph?
    By kenjiro310 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:37 AM
  4. Issue Building Graph in Adjacency Matrix
    By mike.s in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 22nd, 2010, 02:44 PM
  5. Retrieving BFS code's adjacency list
    By moose4204l in forum Collections and Generics
    Replies: 0
    Last Post: October 20th, 2008, 06:30 PM