Docsity

Greedy Algorithms: Dijkstra's Shortest Path and Minimum Waiting Time Scheduling - Prof. Er, Study notes of Computer Science

Toggle sidebar
bg1
Lecture 8
Greedy Algorithms
Single-source shortest path (Dijkstra’s algorithm), Scheduling
Shortest Path Problem
Problem Specification
Input: Weighted, graph G = (V,E), source node s
Weight (length) function w on each edge e in E
Task: Compute the length of the shortest path from s to every node v in V
(Compute the actual shortest paths from s to very node v in V)
Dijkstra’s algorithm
Initialize set of completed nodes S to be {s}
Initialize D[v] to be the length of the shortest path to v where all intermediate nodes are in S
- Selection algorithm: Choose node v in V-S such that D[v] is minimized
Implementation
- Initialize S to be {s}
- Initialize D[v] to be w(s,v) for all v in V-S
- Loop:
- Find minimum D[v] value and add v to S
- Update D[u] for other nodes in V-S based on addition of v to S
Running time analysis
(|V|V|V|2) time if we use a simple linear search to find min element and perform update
Find and update can be reduced to log n operations per edge in the graph if we use a heap
and an array of lists of edges from each node.
- Heap elements based on D[v] value
- Update items in heap using list of edges from v
Each update costs log n time
Updates happen at most once per edge
(|V|E|V| log |V|V|V|)
Which implementation is better when?
Proofs of Correctness
Assume that Dijkstra’s algorithm doesn’t compute the length of all shortest paths from s
Let v be the first node whose shortest path length is computed incorrectly
Let S be the set of nodes whose shortest paths were computed correctly by Dijkstra
prior to adding v to the processed set of nodes.
The shortest path to v must include some node u not in S
Dijkstra’s algorithm has just used the shortest path from s to v using
only nodes in S when it added v to S.
Either u or some node on the shortest path to u must be a node in V-S whose shortest
path contains only nodes in S. Call this node y.
- Two nodes both cannot be on each other’s shortest path
The length of the shortest path to y must be at least that of the length of the path computed
to v or else we would have chosen y instead of v to add at this step.
The length of the path from y to v must be < 0 or else the alternate shortest path has
at least the same length as the one found by Dijkstra’s algorithm.
No path can have negative length, and thus we have a contradiction.
No swap this time, just the derivation of a contradiction.
u (or y) sort of takes the place of the swap element.
We swap the shortest path to s that goes through u with the path that uses only nodes in S.
pf3
pf4

Partial preview of the text

Download Greedy Algorithms: Dijkstra's Shortest Path and Minimum Waiting Time Scheduling - Prof. Er and more Study notes Computer Science in PDF only on Docsity!

Lecture 8 Greedy Algorithms Single-source shortest path (Dijkstra’s algorithm), Scheduling Shortest Path Problem Problem Specification Input: Weighted, graph G = (V,E), source node s Weight (length) function w on each edge e in E Task: Compute the length of the shortest path from s to every node v in V (Compute the actual shortest paths from s to very node v in V) Dijkstra’s algorithm Initialize set of completed nodes S to be {s} Initialize D[v] to be the length of the shortest path to v where all intermediate nodes are in S

  • Selection algorithm: Choose node v in V-S such that D[v] is minimized Implementation
    • Initialize S to be {s}
    • Initialize D[v] to be w(s,v) for all v in V-S
    • Loop:
      • Find minimum D[v] value and add v to S
      • Update D[u] for other nodes in V-S based on addition of v to S Running time analysis (|V|V|V|^2 ) time if we use a simple linear search to find min element and perform update Find and update can be reduced to log n operations per edge in the graph if we use a heap and an array of lists of edges from each node.
      • Heap elements based on D[v] value
      • Update items in heap using list of edges from v Each update costs log n time Updates happen at most once per edge (|V|E|V| log |V|V|V|) Which implementation is better when? Proofs of Correctness Assume that Dijkstra’s algorithm doesn’t compute the length of all shortest paths from s Let v be the first node whose shortest path length is computed incorrectly Let S be the set of nodes whose shortest paths were computed correctly by Dijkstra prior to adding v to the processed set of nodes. The shortest path to v must include some node u not in S Dijkstra’s algorithm has just used the shortest path from s to v using only nodes in S when it added v to S. Either u or some node on the shortest path to u must be a node in V-S whose shortest path contains only nodes in S. Call this node y.
  • Two nodes both cannot be on each other’s shortest path The length of the shortest path to y must be at least that of the length of the path computed to v or else we would have chosen y instead of v to add at this step. The length of the path from y to v must be < 0 or else the alternate shortest path has at least the same length as the one found by Dijkstra’s algorithm. No path can have negative length, and thus we have a contradiction. No swap this time, just the derivation of a contradiction. u (or y) sort of takes the place of the swap element. We swap the shortest path to s that goes through u with the path that uses only nodes in S.

Minimum average (total) waiting time scheduling Problem Specification Input: Set of jobs {1, 2, ..., n} with processing times {ti} Task: Find an ordering of these jobs on a single processor that minimizes T, the sum of their waiting times in the system T =  (time in system for job i) =  ci where ci is the completion time of each job i Greedy Algorithm The Shortest Processing Time First (SPT) Algorithm There are n! different permutations of the n jobs What is a greedy strategy for ordering these jobs? Choose jobs in order of processing times Implementation Sort jobs by processing time: (n log n) Proof of Correctness Assume SPT is not optimal Consider any optimal schedule (permutation) of the n jobs P Let the jobs be numbered by their order in P Assuming that SPT is not optimal, there must be some job i such that ti > ti+ Construct schedule P’ swapping jobs i and i+ The time in the system for all other jobs is identical to P The time in the system for job i in P’ is identical to that of job i+1 in P The time in the system for job i+1 in P’ is strictly less than that of job i in P This is because ti > ti+ This is a contradiction as P was supposed to be optimal Thus, SPT is optimal Alternative proof of correctness If we look at the mathematical sum, we get T = nt 1 + (n-1)t 2 + (n-2)t 3 + ... + 2 tn-1 + tn This is clearly minimized by making t 1 the minimum possible, t 2 the second minimum possible, ... Minimizing average waiting time with release times and preemptions Problem changes: jobs have release times ri and jobs can be preempted. Release times: Job i cannot be processed until time ri (in the above problem, for all i, ri = 0) Preemptions: We can process a job, stop it to process another job, and then resume processing the original job with no penalty Input: Set of jobs {1, 2, ..., n} with processing times {ti} and release times {ri} Task: Find a preemptive schedule of these jobs on a single processor that minimizes T, the sum of their waiting times in the system T =  (time in system for job i) = (ci – ri) Greedy Strategy? Implementation cost? Proof of correctness?

Improved implementation Revised feasibility check: Lemma: A set of n jobs J is feasible if and only if we can construct a feasible sequence including all the jobs in J as follows: Start with an empty schedule of length n. For each job i in J, schedule i at the latest time t in the range [1, min(n,di)] that is free. Proof: Suppose we are unable to add a job with deadline d Let s be the earliest free slot prior to the addition of this job. We know that d<s. Furthermore, all the jobs currently in slots 1 through s-1 also have deadlines < s. Thus we have s jobs with deadlines at most s-1. The set of jobs is infeasible. Other direction obvious. Improved implementation Definitions: For any time slot t, let nt be the largest time slot less than or equal to t that is free (0 if there is none) Define two time slots i and j to be in the same set if ni = nj For a given set of time slots K, define F(K) to be the smallest member of K. Use disjoint set structures to improve efficiency Initialize each time slot to be a singleton set containing only itself Checking feasibility of job j with deadline dj Find set K containing dj If F(K) = 0, reject job j Else Assign job j to F(K) Find set L containing F(k)- Merge K and L Update F(resulting merged set) Analysis: Instead of linear work for each feasibility check, we now have 2 finds and a merge This requires time that is barely larger than constant Running time is now dominated by sorting step and is  (n log n). Almost linear time if data is presorted.