java - Unweighted graphs using Adjacency lists -


i'm new programming , trying learn data structures on own. trying implement unweighted graph class using adjacency lists, having trouble implementing getadjacentvertices method , don't know how addedge method working specially how implement insertatthebeginning method.please me, book using doesn't explains topics.*

enter code here public class graph {     private arraylist<integer> vertices;     private listnode[] edges;     private int vertexcount = 0;     public graph(int vertexcount){         this.vertexcount = vertexcount;         vertices = new arraylist<integer>();         edges = new listnode[vertexcount];         for(int = 0; < vertexcount; i++){             vertices.add(i);             edges[i] = new listnode ();          }     }     public void addedge(int source, int destination){        int = vertices.indexof(source);        int j = vertices.indexof(destination);        if(i != -1 || j != -1){           edges[i].insertatbeginning(destination);           edges[j].insertatbeginning(source);        }     }     public int getnumbervertices() {        return vertexcount;     }     public object getadjacentvertices(int currentvertex) {      } } 

to answer questions,

  • getadjacentvertices(i) can return i-th element of edges.
  • addedge() doesn't have add beginning. unless have specific reason imposing order on neighbor list, graph algorithms not require one.

a few other points:

  • graph vertices traditionally numbered 0 n - 1. unless vertices have payload (names, costs etc.), can away vertices array.
  • consider naming class undirectedgraph make clear graph undirected (that is, whenever v appears in list of u's neighbors, u appears in list of v's neighbors.
  • consider using list<integer> adjacency lists , implement linkedlist. graph algorithms need ability iterate on adjacency lists, not random access (which arraylist provides).
  • addedge() performs no validation. depending on input coming from, might want check 0 ≤ u, v < vertexcount , edge (u, v) not exist.

i included main() method in same class brevity.

import java.util.linkedlist; import java.util.list; import java.util.vector;  public class undirectedgraph {   private vector<list<integer>> edges;   private int vertexcount = 0;    public undirectedgraph(int vertexcount) {     this.vertexcount = vertexcount;     edges = new vector<list<integer>>(vertexcount);     (int = 0; < vertexcount; i++){       edges.addelement(new linkedlist<integer>());     }   }    public void addedge(int u, int v) {     edges.get(u).add(v);     edges.get(v).add(u);   }    public int getnumbervertices() {     return vertexcount;   }    public object getadjacentvertices(int u) {     return edges.get(u);   }      public static void main(string [] args) {     undirectedgraph g = new undirectedgraph(4);     g.addedge(0, 1);     g.addedge(0, 2);     g.addedge(1, 2);     g.addedge(1, 3);     g.addedge(2, 3);      (int = 0; < g.getnumbervertices(); i++) {       system.out.println("neighbors of node " + + ": " + g.getadjacentvertices(i));     }   } } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -