# Dijkstra’s Shortest Path

* Dijksta's algorithm produces the shortest-path tree relative to some distinguished vertex for a weighted graph. With an unweighted graph, simply preform BFS with previous node until the target is found. Reverse the path with backtracking to identify this path.

## Shortest-Path Tree vs Minimum Spanning Tree

* Both are some spanning tree from a graph G.
* A minimum spanning tree is more generic the shortest path.
  * A minimum spanning tree prioritizes connecting all the nodes in a graph such that final sum of the collected edges is minimal.
  * A shortest path tree looks at specific vertex, and maximize the a spanning tree such that it minimal to from some starting point to any other vertex in the tree.
  * So the main difference is that a shortest path tree produces a minimal tree relative to a vertex while a minimum spanning tree looks at the bigger picture to produce an overall minimal network.

## On Paper:

1. For best visualization and efficiency:
2. Start with the distinguished vertex or node.
3. Follow the procedure, by writing down the relative costs next to the node on paper.
4. When moving onto the new node, only consider the nodes it connects to and update if need be.
5. Once you’re finished, every node will have an associated cost.&#x20;

<http://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/04DemoDijkstra.pdf>

## English Algorithm

1. Start with a distinguished vertex&#x20;
2. Cumulatively update the costs of all the nodes connected relatively to the currently active node.
   1. Update the connected node with the new minimum cost.
3. Augment to the next lowest costing vertex. This is now the current active node 1.
4. Mark active node as visited. Once all nodes are visited, the algorithm terminates. Otherwise,&#x20;
5. See 2.

## Less English Algorithm

* Define a priority queue, that will be used as our main selector for which node to chose. Define its comparitor to sort by cumulative weights of each node passed in.
* Given a starting vertex s, place all outgoing nodes of s into a priority queue with cumulative weights (starting from 0). Mark s as visited. Mark the remainder as visiting.
* Select q\_cur = q.top() -> which will be the minimum edge. Mark it as visited. Call q.pop() to delete it.
* For each edge i adjacent to q\_cur, that is also not visited:
  * if its weight in the priority queue is greater than the weight defined by the cur\_node + edge weight adjacent, then update it the cumulative node in priority queue.
  * If it is a new weight, then add it to the priority queue with the weight = cumulative weight + current edge weight.
* Continue until q is empty

## Psuedo Code

<http://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/04GreedyAlgorithmsII.pdf>

## Data-structures and Justification

* **Priority-queue**: Cumulative node weights will be continually added into the data structure, as well as removed. We want to be able to maintain and $$O(logn)$$ removal, and insertion.

## Limitations

* Negative edge lengths in a graph can potentially provide a non-optimal solution.
