-1

I have this graph

enter image description here

in graph S and P take values{1,2,3,4,5}

I want to represent this graph in java

can you help me with idea?

thank you in advance

Razib
  • 10,965
  • 11
  • 53
  • 80
FRIDI Mourad
  • 87
  • 1
  • 6
  • 16
  • The easiest thing is to find a Java graph library: http://stackoverflow.com/questions/51574/good-java-graph-algorithm-library – duffymo Mar 03 '15 at 22:09

2 Answers2

2

You can also use adjancy list using the following code snippet -

    int n;
    List<Integer>[] adj;
    AdjacencyLists(int n0) {
        n = n0;
        adj = (List<Integer>[])new List[n];
        for (int i = 0; i < n; i++) 
            adj[i] = new ArrayStack<Integer>(Integer.class);
    }  

Then add edge using the function -

void addEdge(int i, int j) {
        adj[i].add(j);
    } 

Or can remove edge -

void removeEdge(int i, int j) {
        Iterator<Integer> it = adj[i].iterator();
        while (it.hasNext()) {
            if (it.next() == j) {
                it.remove();
                return;
            }
        }    
    }  

Or even check if a edge is exists -

boolean hasEdge(int i, int j) {
        return adj[i].contains(j);
    }  

You can found more detail about adjacency list here.

Or you may use adjacency matrix

Hope it will help.
Thanks a lot

Razib
  • 10,965
  • 11
  • 53
  • 80
1

Try an Incidence Matrix, it's one of the easiest ways in my opinion.

http://en.wikipedia.org/wiki/Incidence_matrix