Home > Programming > Adjacency list represention of graph in Java

Adjacency list represention of graph in Java


Here is the program for adjacency list representation of graphs using java. If you are new to Graphs please read about graphs here before reading the rest of this post.

I used a List data structure inside the graph. I think much explanation is not required because it is very simple.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author Vasanth Raja Chittampally
 */
class List{
    int node;
    List next;
}

class Node {
    int node;
    List adjList;
}

public class Graphs {
    void insertVertices() throws IOException
    {
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter number of nodes in a graph: ");
        int n=(Integer.parseInt(br.readLine()));
        Node g[]=new Node[n];
        for (int i = 0; i < n; i++) {
            g[i]=new Node();
            g[i].node=i+1;
            g[i].visit=false;
            while(true)
            {
                System.out.println("Enter zero to stop");
                System.out.println("Enter adjacent nodes to node "+(i+1)+" :");
                 int adjVertex=Integer.parseInt((br.readLine()));
                 if(adjVertex==0)
                     break;
                 else
                 {
                        List l=new List();
                        l.node=adjVertex;
                        l.next=null;
                        if(g[i].adjList==null)
                        {
                            g[i].adjList=l;
                        }
                        else
                        {
                            List p=g[i].adjList;
                            while(p.next!=null){
                                p=p.next;
                            }
                            p.next=l;
                        }
                 }
            }
        }
        System.out.println("Adjacency matrix representation");
        for (int i = 0; i < n; i++) {
            System.out.print(g[i].node+" --> ");
            List l=g[i].adjList;
            while(l!=null)
            {
                System.out.print(l.node+" --> ");
                l=l.next;
            }
            System.out.println("");
        }
    }

    public static void main(String[] args) throws IOException {
             new Graphs().insertVertices();
    }

}
Advertisements
  1. eddie
    January 25, 2012 at 8:31 pm

    The line
    g[i].visit=false;
    should be left out
    eddie

  2. Sivakumar Balasubramaniam
    March 8, 2012 at 12:28 pm

    In Node Class add

    Boolean visit;

    ———————–
    Nice program

  1. January 2, 2011 at 1:57 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: