0

I have a project : Graph coloring using genetic algorithms. Until now i wrote the build of the graph. But i am not sure if i did it right. How i show my graph?

import java.io.*;
import java.util.*;
import java.util.LinkedList;


public class Graph {

    private int V;                      // Nr. de noduri
    private LinkedList<Integer> a[];    // Lista de adiacente

    Graph(int v)
    {
        V = v;
        a = new LinkedList[v];
        for (int i=0; i < v; ++i)
            a[i] = new LinkedList();
    }

    void link(int v,int l)
    {
        a[v].add(l);                    // deoarece e graf neorientat adaugam legaturi in ambele sensuri
        a[l].add(v);  
    }



    public static void main(String args[])
    {
        Graph g1 = new Graph(5);
        g1.link(0, 1);
        g1.link(0, 2);
        g1.link(1, 2);
        g1.link(1, 3);
        g1.link(2, 3);
        g1.link(3, 4);
        System.out.println("Graph:"+g1);

    }
}
StringDot
  • 25
  • 5
  • You can use gephi toolkit for visualise your graph. Or save your graph into a file then open the file using gephi. It would be much more easier for you – Abdullah Tellioglu Apr 28 '17 at 18:25

1 Answers1

0

Objects in java have a toString() method. If you want to print the graph easily you can add your own toString() implementation to your graph class. Try something like this:

@Override
public String toString() {
   for (int i=0; i<a.size(); i++) {
      System.out.println(i + ": " + a[i] + "\n");
   }
}

The @Override isn't 100% necessary but its definitely recommended: see

Community
  • 1
  • 1
gwcoderguy
  • 392
  • 1
  • 13