java - Adjacency Lists for graph representation using space O(number of edges) -


i'm trying represent graph(connected-non directed-without weights) in java using adjacency lists (the space represent graph has o(m) m number of edges) in order find informations usings bfs. information graph txt called graph.txt. i'm not sure if use space o(m) save graph , i'm not sure if way save in order use bfs.

public class vertex  {     boolean visited = false;     int number;     arraylist<integer> adjlist;     public vertex(int i) {         this.number= i;         adjlist = new arraylist<integer>();      }     public void addneighbor(int i) {         this.adjlist.add(i);     } } 

yes, representation use o(m) space. have 2 different suggestions, given representation.

1. if want represent vertex class, have it's list of adjacent verticies list<vertex> instead of list<integer>

or

2. since vertex class not seem hold information other integer value of vertex, why not use integer itself? graph represented as

arraylist<arraylist<integer>> graph; 

where graph[i] list of vertices connected vertex makes don't have find matches integer id vertex instance.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -