I have an assignment to implement Dijkstra's Algorithm. We're given skeleton code that inputs the graph as an adjacency matrix, but told our solution must run in O(MlogN) {M-edges, N-vertices}. I see how a PQ and list structure would accomplish this, but I don't see how I can avoid an O(N^2) solution in my case. The only way to get out of the matrix is to iterate through every row and column...right?
2
votes
1 Answers
1
votes
Wrong. Your task isn't to perform manipulations on every value in the matrix but to find shortest path between 2 nodes. Matrix in your case is a medium to hold the data and it doesn't change the complexity of the algorithm. The O(MlogN) algorithm is available freely, e.g. here: Dijkstra's algorithm.