Welcome to Dream.In.Code
Become a Java Expert!

Join 150,381 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,368 people online right now. Registration is fast and FREE... Join Now!




how to create a graph with adjacency list

 
Reply to this topicStart new topic

how to create a graph with adjacency list, help me to start

EquinoX
19 Feb, 2008 - 08:25 PM
Post #1

D.I.C Head
**

Joined: 19 Jan, 2007
Posts: 63


My Contributions
I am going to create a graph representation using a adjacency list. How do I represent the vertices and the connected vertices??? I am thinking of using a set of hash map to represent all the vertices and linked list to represent the adjacency vertices. But my problem comes to how do I connect from the vertices in the hash map into the linked list?? Say that in the hash map I have the vertices number from 0 up to n as the key, and what is the value here??
User is offlineProfile CardPM
+Quote Post

Jayman
RE: How To Create A Graph With Adjacency List
19 Feb, 2008 - 09:31 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks.
User is offlineProfile CardPM
+Quote Post

EquinoX
RE: How To Create A Graph With Adjacency List
20 Feb, 2008 - 07:10 AM
Post #3

D.I.C Head
**

Joined: 19 Jan, 2007
Posts: 63


My Contributions
here's the code that I have so far:

CODE

public class Graph {

    private ArrayList<Integer> vertices;
    private LinkedList<Integer>[] edges;
    private int numVertices = 0;

    public Graph(int numVertices) {
        this.numVertices = numVertices;
        vertices = new ArrayList<Integer>();
        edges = (LinkedList<Integer>[]) new LinkedList[numVertices];
        for (int i = 0; i < numVertices; i++) {
            vertices.add(i);
            edges[i] = new LinkedList<Integer>();
        }
    }
public void addEdge(int source, int destination) {
        int i = vertices.indexOf(source);
        int j = vertices.indexOf(destination);
        if (i != -1 || j != -1) {
            edges[i].addFirst(destination);
            edges[j].addFirst(source);
        }
    }


I am just worried that this won't work, any suggestions??
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:40PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month