Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Java Graph Show

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);

    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!