Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Some questions/concern when using adjacency matrix to implement a graph in Java

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Some questions/concern when using adjacency matrix to implement a graph in Java

    I'm a newer in Java and now about to use adjacency matrix(2D array) to implement a graph, but encountered some problems, pls help me,thanks. following are partly codes.

    // following codes create a empty graph
    public AdjMatrix() {
    private boolean[][] adjacencymatrix; /*Define a 2D array to store adjacency matrix*/
    private int vertices_numb; /*Number of vertices in the graph */
    T[] vertices; /*To store the values of vertices*/
    vertices_numb=0 /*set the graph to empty*/
    } // end of AdjMatrix()


    //Following code is for adding a vertex.
    public void addVertex(T vertLabel) {
    if (vertices_numb == 0) //If the graph is empty, print related information and assign value 0 to adjacency matrix[0][0]
    System.out.println("Graph is empty!");
    adjacencymatrix[0][0]=0;
    vertices[0]=vertLabel; //To save the vertex
    vertices_numb=vertices_numb+1; //to count the number of vertex
    else
    for(int i=0;i<=vertices_numb;i++) {
    vertices[vertices_numb]=vertLabel;
    adjacencymatrix[i][vertices_num]= how does program know they are connected between 2 nodes?
    vertices_numb=vertices_numb+1;
    }
    } // end of addVertex()

    My problems are:
    1) How does program know if they are connected between two nodes?
    2) When adding a vertex, do we need to consider the connection between this one with the nodes exist?
    Many thanks.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,263
    Thanks
    52
    Thanked 2,275 Times in 2,246 Posts

    Default Re: Some questions/concern when using adjacency matrix to implement a graph in Java

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    My problems are:
    Sorry, I don't see any java programming problems here. Those questions are about the design logic that would be for any programming language.
    If you have a design and want help writing code for that design, please post it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Adjacency list graph
    By custurd122000 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 4th, 2014, 07:55 PM
  2. Concern about use of });
    By runkerr in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 13th, 2013, 05:53 AM
  3. Java Bar graph takes user input to create graph?
    By kenjiro310 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:37 AM
  4. Issue Building Graph in Adjacency Matrix
    By mike.s in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 22nd, 2010, 02:44 PM