0

I have an assignment on Dijkstra's algorithm, but the question has me confused about the input. It asks me to find shortest and second shortest paths, and that part I have figured out, but how do I even start with the graph has me troubled. The question says the input has to be read from a file and the file contains the number of nodes and the weight between two nodes. Weight between two nodes should be 1 to 9, and can use 0 to indicate a path that doesn't exist. Now my question is what has to be the contents of the file? I was able to understand Dijkstra's algorithm where the input was a 2d array that represents the graph. Can someone clarify what is expected from this question? Like what the source file should contain.

5
  • I think input format should be defined in the assignment, there are many ways how to represent a graph.. Commented Jun 15, 2020 at 10:52
  • @Ecto The input is read from a file and contains the number of nodes and the weight between two nodes. Weight between two nodes should be within 1 to 9. You may use 0 to indicate a path that doesn’t exist. This is what the assignment says for input format. That's it. Commented Jun 15, 2020 at 10:54
  • Do you create the file on your own? Commented Jun 15, 2020 at 10:55
  • @Ecto Yes........ Commented Jun 15, 2020 at 10:56
  • @wakanada you can represent a weighted Graph as a Matrix, where the entry (i,j) is the weight between Node i and j. When it is an undirected graph it is a symmetrical matrix. If there is no path from i to j use 0. Commented Jun 15, 2020 at 11:01

1 Answer 1

0

You are probably supposed to create file like this: (example - there are many ways to do it)

4   # number of nodes (from 1 to 4)
1 2 3   # means edge from node 1 to node 2 with weight 3
2 3 1   # means edge from node 2 to node 3 with weight 1
...

this would correspond to 2d matrix like this:

0 3 0 0
0 0 1 0
0 0 0 0
0 0 0 0
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.