Generic Graph using Adjacency matrix - Java

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












The main purpose of representing Graph using adjacency matrix method is, to check the vertex and its neighbor's existence in constant time proportional to $mathcalO(n)$.



In the various tutorials I have seen, Graphs contain only integer vertices and it becomes straight forward to represent them in a $v times v$ integer 2D array to map the vertices.



In real world, we might have to store a custom type Object as the graph vertex. For this, I have created a Graph implementation with Adjacency matrix. Can you please let me know of any feedback / improvements?



//V - type of Object stored on graph vertices
public class GraphAM<V>

//Maps vertex with its adjacency matrix index. O(1) to retrieve index of a vertex
private Map<V, Integer> vertices;
//To get vertex using index at O(1) time
private List<V> verticesLookup;

//adjacency matrix
private int adj;

private int index;

public GraphAM(int numVertices)
adj = new int[numVertices][numVertices];
index = 0;
vertices = new HashMap<>();
verticesLookup = new ArrayList<>();


public void addEdge(V from, V to)
addVertex(from);
addVertex(to);

int fromIndex = vertices.get(from);
int toIndex = vertices.get(to);
adj[fromIndex][toIndex] = 1;


private void addVertex(V v)
if(!vertices.containsKey(v))
vertices.put(v, index);
verticesLookup.add(index, v);
index++;



public void bfs(V start)
Queue<V> queue = new LinkedList<>();
boolean visited = new boolean[vertices.size()];

queue.add(start);
int index = vertices.get(start);
visited[index] = true;

while(!queue.isEmpty())
V v = queue.poll();
System.out.print(v + " ");

List<V> adjacentVertices = getAdjacentVertices(v);
for(V a : adjacentVertices)
int adjInd = vertices.get(a);
if(!visited[adjInd])
queue.add(a);
visited[adjInd] = true;







public void dfs(V start)
boolean visited = new boolean[vertices.size()];
dfs(start, visited);


private void dfs(V v, boolean visited)
System.out.print(v + " ");
int index = vertices.get(v);
visited[index] = true;

List<V> adjacentVertices = getAdjacentVertices(v);
for(V a : adjacentVertices)
int aIndex = vertices.get(a);
if(!visited[aIndex])
dfs(a, visited);




private List<V> getAdjacentVertices(V v)
int index = vertices.get(v);
List<V> result = new ArrayList<>();
for(int i=0; i<adj[index].length; i++)
if(adj[index][i] == 1)
result.add(verticesLookup.get(i));


return result;





Main class



class Main 

public static void main(String args)
GraphAM<Integer> g = new GraphAM<>(4);

g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

System.out.println("Following is Breadth First Traversal "+
"(starting from vertex 2)");

g.bfs(2);

System.out.println("nFollowing is Depth First Traversal "+
"(starting from vertex 2)");

g = new GraphAM<>(4);

g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

g.dfs(2);









share|improve this question



























    up vote
    3
    down vote

    favorite












    The main purpose of representing Graph using adjacency matrix method is, to check the vertex and its neighbor's existence in constant time proportional to $mathcalO(n)$.



    In the various tutorials I have seen, Graphs contain only integer vertices and it becomes straight forward to represent them in a $v times v$ integer 2D array to map the vertices.



    In real world, we might have to store a custom type Object as the graph vertex. For this, I have created a Graph implementation with Adjacency matrix. Can you please let me know of any feedback / improvements?



    //V - type of Object stored on graph vertices
    public class GraphAM<V>

    //Maps vertex with its adjacency matrix index. O(1) to retrieve index of a vertex
    private Map<V, Integer> vertices;
    //To get vertex using index at O(1) time
    private List<V> verticesLookup;

    //adjacency matrix
    private int adj;

    private int index;

    public GraphAM(int numVertices)
    adj = new int[numVertices][numVertices];
    index = 0;
    vertices = new HashMap<>();
    verticesLookup = new ArrayList<>();


    public void addEdge(V from, V to)
    addVertex(from);
    addVertex(to);

    int fromIndex = vertices.get(from);
    int toIndex = vertices.get(to);
    adj[fromIndex][toIndex] = 1;


    private void addVertex(V v)
    if(!vertices.containsKey(v))
    vertices.put(v, index);
    verticesLookup.add(index, v);
    index++;



    public void bfs(V start)
    Queue<V> queue = new LinkedList<>();
    boolean visited = new boolean[vertices.size()];

    queue.add(start);
    int index = vertices.get(start);
    visited[index] = true;

    while(!queue.isEmpty())
    V v = queue.poll();
    System.out.print(v + " ");

    List<V> adjacentVertices = getAdjacentVertices(v);
    for(V a : adjacentVertices)
    int adjInd = vertices.get(a);
    if(!visited[adjInd])
    queue.add(a);
    visited[adjInd] = true;







    public void dfs(V start)
    boolean visited = new boolean[vertices.size()];
    dfs(start, visited);


    private void dfs(V v, boolean visited)
    System.out.print(v + " ");
    int index = vertices.get(v);
    visited[index] = true;

    List<V> adjacentVertices = getAdjacentVertices(v);
    for(V a : adjacentVertices)
    int aIndex = vertices.get(a);
    if(!visited[aIndex])
    dfs(a, visited);




    private List<V> getAdjacentVertices(V v)
    int index = vertices.get(v);
    List<V> result = new ArrayList<>();
    for(int i=0; i<adj[index].length; i++)
    if(adj[index][i] == 1)
    result.add(verticesLookup.get(i));


    return result;





    Main class



    class Main 

    public static void main(String args)
    GraphAM<Integer> g = new GraphAM<>(4);

    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 2);
    g.addEdge(2, 0);
    g.addEdge(2, 3);
    g.addEdge(3, 3);

    System.out.println("Following is Breadth First Traversal "+
    "(starting from vertex 2)");

    g.bfs(2);

    System.out.println("nFollowing is Depth First Traversal "+
    "(starting from vertex 2)");

    g = new GraphAM<>(4);

    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 2);
    g.addEdge(2, 0);
    g.addEdge(2, 3);
    g.addEdge(3, 3);

    g.dfs(2);









    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      The main purpose of representing Graph using adjacency matrix method is, to check the vertex and its neighbor's existence in constant time proportional to $mathcalO(n)$.



      In the various tutorials I have seen, Graphs contain only integer vertices and it becomes straight forward to represent them in a $v times v$ integer 2D array to map the vertices.



      In real world, we might have to store a custom type Object as the graph vertex. For this, I have created a Graph implementation with Adjacency matrix. Can you please let me know of any feedback / improvements?



      //V - type of Object stored on graph vertices
      public class GraphAM<V>

      //Maps vertex with its adjacency matrix index. O(1) to retrieve index of a vertex
      private Map<V, Integer> vertices;
      //To get vertex using index at O(1) time
      private List<V> verticesLookup;

      //adjacency matrix
      private int adj;

      private int index;

      public GraphAM(int numVertices)
      adj = new int[numVertices][numVertices];
      index = 0;
      vertices = new HashMap<>();
      verticesLookup = new ArrayList<>();


      public void addEdge(V from, V to)
      addVertex(from);
      addVertex(to);

      int fromIndex = vertices.get(from);
      int toIndex = vertices.get(to);
      adj[fromIndex][toIndex] = 1;


      private void addVertex(V v)
      if(!vertices.containsKey(v))
      vertices.put(v, index);
      verticesLookup.add(index, v);
      index++;



      public void bfs(V start)
      Queue<V> queue = new LinkedList<>();
      boolean visited = new boolean[vertices.size()];

      queue.add(start);
      int index = vertices.get(start);
      visited[index] = true;

      while(!queue.isEmpty())
      V v = queue.poll();
      System.out.print(v + " ");

      List<V> adjacentVertices = getAdjacentVertices(v);
      for(V a : adjacentVertices)
      int adjInd = vertices.get(a);
      if(!visited[adjInd])
      queue.add(a);
      visited[adjInd] = true;







      public void dfs(V start)
      boolean visited = new boolean[vertices.size()];
      dfs(start, visited);


      private void dfs(V v, boolean visited)
      System.out.print(v + " ");
      int index = vertices.get(v);
      visited[index] = true;

      List<V> adjacentVertices = getAdjacentVertices(v);
      for(V a : adjacentVertices)
      int aIndex = vertices.get(a);
      if(!visited[aIndex])
      dfs(a, visited);




      private List<V> getAdjacentVertices(V v)
      int index = vertices.get(v);
      List<V> result = new ArrayList<>();
      for(int i=0; i<adj[index].length; i++)
      if(adj[index][i] == 1)
      result.add(verticesLookup.get(i));


      return result;





      Main class



      class Main 

      public static void main(String args)
      GraphAM<Integer> g = new GraphAM<>(4);

      g.addEdge(0, 1);
      g.addEdge(0, 2);
      g.addEdge(1, 2);
      g.addEdge(2, 0);
      g.addEdge(2, 3);
      g.addEdge(3, 3);

      System.out.println("Following is Breadth First Traversal "+
      "(starting from vertex 2)");

      g.bfs(2);

      System.out.println("nFollowing is Depth First Traversal "+
      "(starting from vertex 2)");

      g = new GraphAM<>(4);

      g.addEdge(0, 1);
      g.addEdge(0, 2);
      g.addEdge(1, 2);
      g.addEdge(2, 0);
      g.addEdge(2, 3);
      g.addEdge(3, 3);

      g.dfs(2);









      share|improve this question













      The main purpose of representing Graph using adjacency matrix method is, to check the vertex and its neighbor's existence in constant time proportional to $mathcalO(n)$.



      In the various tutorials I have seen, Graphs contain only integer vertices and it becomes straight forward to represent them in a $v times v$ integer 2D array to map the vertices.



      In real world, we might have to store a custom type Object as the graph vertex. For this, I have created a Graph implementation with Adjacency matrix. Can you please let me know of any feedback / improvements?



      //V - type of Object stored on graph vertices
      public class GraphAM<V>

      //Maps vertex with its adjacency matrix index. O(1) to retrieve index of a vertex
      private Map<V, Integer> vertices;
      //To get vertex using index at O(1) time
      private List<V> verticesLookup;

      //adjacency matrix
      private int adj;

      private int index;

      public GraphAM(int numVertices)
      adj = new int[numVertices][numVertices];
      index = 0;
      vertices = new HashMap<>();
      verticesLookup = new ArrayList<>();


      public void addEdge(V from, V to)
      addVertex(from);
      addVertex(to);

      int fromIndex = vertices.get(from);
      int toIndex = vertices.get(to);
      adj[fromIndex][toIndex] = 1;


      private void addVertex(V v)
      if(!vertices.containsKey(v))
      vertices.put(v, index);
      verticesLookup.add(index, v);
      index++;



      public void bfs(V start)
      Queue<V> queue = new LinkedList<>();
      boolean visited = new boolean[vertices.size()];

      queue.add(start);
      int index = vertices.get(start);
      visited[index] = true;

      while(!queue.isEmpty())
      V v = queue.poll();
      System.out.print(v + " ");

      List<V> adjacentVertices = getAdjacentVertices(v);
      for(V a : adjacentVertices)
      int adjInd = vertices.get(a);
      if(!visited[adjInd])
      queue.add(a);
      visited[adjInd] = true;







      public void dfs(V start)
      boolean visited = new boolean[vertices.size()];
      dfs(start, visited);


      private void dfs(V v, boolean visited)
      System.out.print(v + " ");
      int index = vertices.get(v);
      visited[index] = true;

      List<V> adjacentVertices = getAdjacentVertices(v);
      for(V a : adjacentVertices)
      int aIndex = vertices.get(a);
      if(!visited[aIndex])
      dfs(a, visited);




      private List<V> getAdjacentVertices(V v)
      int index = vertices.get(v);
      List<V> result = new ArrayList<>();
      for(int i=0; i<adj[index].length; i++)
      if(adj[index][i] == 1)
      result.add(verticesLookup.get(i));


      return result;





      Main class



      class Main 

      public static void main(String args)
      GraphAM<Integer> g = new GraphAM<>(4);

      g.addEdge(0, 1);
      g.addEdge(0, 2);
      g.addEdge(1, 2);
      g.addEdge(2, 0);
      g.addEdge(2, 3);
      g.addEdge(3, 3);

      System.out.println("Following is Breadth First Traversal "+
      "(starting from vertex 2)");

      g.bfs(2);

      System.out.println("nFollowing is Depth First Traversal "+
      "(starting from vertex 2)");

      g = new GraphAM<>(4);

      g.addEdge(0, 1);
      g.addEdge(0, 2);
      g.addEdge(1, 2);
      g.addEdge(2, 0);
      g.addEdge(2, 3);
      g.addEdge(3, 3);

      g.dfs(2);











      share|improve this question












      share|improve this question




      share|improve this question








      edited Mar 24 at 11:05
























      asked Mar 24 at 10:41









      sanbhat

      1185




      1185




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          • You can simplify the mapping you are using to get from vertex to it's associated object. Instead of using a List<V>, which can only guarantee $mathcalO(n)$ lookup time, you could just use an array.



          • Comments restating what's known should be removed, comments that can be made obsolete through refactoring should prompt that refactoring:



            public class GraphAM<V> {
            private Map<V, Integer> vertexToIndex;
            private V indexToVertex;

            private int adjacencyMatrix;
            private int index;


          • vertices (aka vertexToIndex can be eagerly initialized.


          • all private fields (apart from index) should be marked final.


          • Instead of passing numVertices to the constructor, you should consider passing a Collection<V>, which removes the need to deal with adding vertices in addEdge.


          • Finally instead of using a boolean for visited, a Set<V> is significantly more obvious. It might come with a minor performance penalty though.


          • Finally the search functions do not search for anything... they're just performing an exhaustive traversal and are accordingly useless in their current form...






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190359%2fgeneric-graph-using-adjacency-matrix-java%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            • You can simplify the mapping you are using to get from vertex to it's associated object. Instead of using a List<V>, which can only guarantee $mathcalO(n)$ lookup time, you could just use an array.



            • Comments restating what's known should be removed, comments that can be made obsolete through refactoring should prompt that refactoring:



              public class GraphAM<V> {
              private Map<V, Integer> vertexToIndex;
              private V indexToVertex;

              private int adjacencyMatrix;
              private int index;


            • vertices (aka vertexToIndex can be eagerly initialized.


            • all private fields (apart from index) should be marked final.


            • Instead of passing numVertices to the constructor, you should consider passing a Collection<V>, which removes the need to deal with adding vertices in addEdge.


            • Finally instead of using a boolean for visited, a Set<V> is significantly more obvious. It might come with a minor performance penalty though.


            • Finally the search functions do not search for anything... they're just performing an exhaustive traversal and are accordingly useless in their current form...






            share|improve this answer

























              up vote
              2
              down vote



              accepted










              • You can simplify the mapping you are using to get from vertex to it's associated object. Instead of using a List<V>, which can only guarantee $mathcalO(n)$ lookup time, you could just use an array.



              • Comments restating what's known should be removed, comments that can be made obsolete through refactoring should prompt that refactoring:



                public class GraphAM<V> {
                private Map<V, Integer> vertexToIndex;
                private V indexToVertex;

                private int adjacencyMatrix;
                private int index;


              • vertices (aka vertexToIndex can be eagerly initialized.


              • all private fields (apart from index) should be marked final.


              • Instead of passing numVertices to the constructor, you should consider passing a Collection<V>, which removes the need to deal with adding vertices in addEdge.


              • Finally instead of using a boolean for visited, a Set<V> is significantly more obvious. It might come with a minor performance penalty though.


              • Finally the search functions do not search for anything... they're just performing an exhaustive traversal and are accordingly useless in their current form...






              share|improve this answer























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                • You can simplify the mapping you are using to get from vertex to it's associated object. Instead of using a List<V>, which can only guarantee $mathcalO(n)$ lookup time, you could just use an array.



                • Comments restating what's known should be removed, comments that can be made obsolete through refactoring should prompt that refactoring:



                  public class GraphAM<V> {
                  private Map<V, Integer> vertexToIndex;
                  private V indexToVertex;

                  private int adjacencyMatrix;
                  private int index;


                • vertices (aka vertexToIndex can be eagerly initialized.


                • all private fields (apart from index) should be marked final.


                • Instead of passing numVertices to the constructor, you should consider passing a Collection<V>, which removes the need to deal with adding vertices in addEdge.


                • Finally instead of using a boolean for visited, a Set<V> is significantly more obvious. It might come with a minor performance penalty though.


                • Finally the search functions do not search for anything... they're just performing an exhaustive traversal and are accordingly useless in their current form...






                share|improve this answer













                • You can simplify the mapping you are using to get from vertex to it's associated object. Instead of using a List<V>, which can only guarantee $mathcalO(n)$ lookup time, you could just use an array.



                • Comments restating what's known should be removed, comments that can be made obsolete through refactoring should prompt that refactoring:



                  public class GraphAM<V> {
                  private Map<V, Integer> vertexToIndex;
                  private V indexToVertex;

                  private int adjacencyMatrix;
                  private int index;


                • vertices (aka vertexToIndex can be eagerly initialized.


                • all private fields (apart from index) should be marked final.


                • Instead of passing numVertices to the constructor, you should consider passing a Collection<V>, which removes the need to deal with adding vertices in addEdge.


                • Finally instead of using a boolean for visited, a Set<V> is significantly more obvious. It might come with a minor performance penalty though.


                • Finally the search functions do not search for anything... they're just performing an exhaustive traversal and are accordingly useless in their current form...







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Mar 24 at 11:03









                Vogel612♦

                20.9k345124




                20.9k345124






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190359%2fgeneric-graph-using-adjacency-matrix-java%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Python Lists

                    Aion

                    JavaScript Array Iteration Methods