-2

Why do we see 2 different visualizations for the questions requiring Djikstras's algorithm as a solution?

In one case I see nodes connected to each other, the other format is a 2d array. Is there a way to convert or mentally visualize the 2d array as connected nodes?

I'd like to know so I am clear on my approach to solving the question.

See references here:

Node Approach to shortest path, which presents code like this:

class Node {
    private Map<Node, Integer> adjacentNodes = new HashMap<>();
    // ...
}

2D Array Approach to shortest path which starts with a nodes visualisation, and then moves to this 2D array:

int graph[][] = new int[][] {
    {0, 4, 0, 0, 7},
    {4, 0, 1, 2, 0},
    {0, 1, 0, 6, 0},
    {0, 2, 6, 0, 0},
    {7, 0, 0, 0, 0}
};
2
  • Does en.wikipedia.org/wiki/Graph_theory#Representation answer your question? The two representations there are visual and tabular which I think correspond to the two concepts in your question. Commented Sep 10, 2024 at 6:59
  • it helps, but I need to wrap my head around all of this info. Commented Sep 14, 2024 at 0:03

1 Answer 1

0

The two references use two different, but common, representations of a graph:

  1. Adjacency list

    The referenced code looks like this:

    class Node {
        private Map<Node, Integer> adjacentNodes = new HashMap<>();
        // ...
    }
    

    ... and then calling the Node constructor and methods to actually create the nodes and edges of the actual graph.

  2. Adjacency matrix

    The referenced code looks like this:

    int graph[][] = new int[][] {
        {0, 4, 0, 0, 7},
        {4, 0, 1, 2, 0},
        {0, 1, 0, 6, 0},
        {0, 2, 6, 0, 0},
        {7, 0, 0, 0, 0}
    };
    

The two data structures are documented at Wikipedia:

  • Adjacency matrix:

    For a simple graph with vertex set 𝑈 = {𝑢1, …, 𝑢𝑛}, the adjacency matrix is a square 𝑛 × 𝑛 matrix 𝐴 such that its element 𝐴𝑖𝑗 is one when there is an edge from vertex 𝑢𝑖 to vertex 𝑢𝑗, and zero when there is no edge.

    [...]

    It is also possible to store edge weights directly in the elements of an adjacency matrix.

  • Adjacency list:

    An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighbouring vertices or edges.

    The article goes on with details about how this can be implemented, as there are quite a few ways to do it.

These two structures have their own advantages and disadvantages. Both Wikipedia articles mention several of these trade-offs, including space and time efficiency concerns.

I'd like to add that in this particular matrix representation there is no way to encode an edge that has a weight of zero, as 0 is reserved for indicating there is no edge.

For the purpose of Dijkstra's algorithm, it will in general be more efficient to use the adjacency list approach. A boundary case is when the graph has edges between (almost) any pair of vertices -- in that case an adjacency matrix might be more useful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.