Final answer:
For uniformly weighted edges, Breadth-First Search (BFS) is used to find the shortest path with complexity O(|N| + |E|). For non-uniform weights, Dijkstra's algorithm is appropriate with complexity O(|N| + W|E|) or O(W|N| + |E|), depending on if W is included in complexity. Adjacency lists are used as the graph representation to attain these complexities.
Step-by-step explanation:
Algorithms for Shortest Path in Directed Graphs
Given a directed graph G=(N,E) with weights as integers 1 to W, we approach the shortest path problem differently for each case:
P4.1. When W=1 (Uniform Weight)
In this case, the shortest path is the one with the fewest edges. We can use Breadth-First Search (BFS), which achieves O(|N| + |E|) complexity because it visits each node and edge only once. Here is the algorithm:
-
- Start at node n and initialize a queue with this node.
-
- Mark n as visited and initialize the path length to 0 for n.
-
- While the queue is not empty, dequeue a node and:
-
- Examine its neighbors. If it's the target node m, return the path length.
-
- If a neighbor hasn't been visited, mark it as visited and enqueue it along with the updated path length.
P4.2. With Arbitrary Weights (W=1…W)
For arbitrary weights, we can use Dijkstra's algorithm, which has O(|N| + W|E|) complexity with a priority queue. This algorithm iterates over each node's edges at most once, and keeps the shortest known distance to every node, hence satisfying the given complexity.
P4.3. When Considering W in Complexity
If W is considered in the computation of complexity, we can still use Dijkstra, but the analysis of complexity becomes O(W|N| + |E|), since in the worst-case scenario, the weight can contribute as much to the complexity as the number of nodes itself.
For all cases, the use of adjacency lists as the graph representation is recommended to achieve the stated complexities due to their efficient handling of sparse graphs.