INF421, Lecture 2 Queues
Leo Liberti LIX, ´ Ecole Polytechnique, France
INF421, Lecture 2 – p. 1
INF421, Lecture 2 Queues Leo Liberti LIX, Ecole Polytechnique, - - PowerPoint PPT Presentation
INF421, Lecture 2 Queues Leo Liberti LIX, Ecole Polytechnique, France INF421, Lecture 2 p. 1 Course Objective : to teach you some data structures and associated algorithms Evaluation : TP not en salle info le 16 septembre, Contrle
Leo Liberti LIX, ´ Ecole Polytechnique, France
INF421, Lecture 2 – p. 1
Objective: to teach you some data structures and associated
algorithms
Evaluation: TP noté en salle info le 16 septembre, Contrôle à la fin.
Note: max(CC, 3
4CC + 1 4TP)
Organization: fri 26/8, 2/9, 9/9, 16/9, 23/9, 30/9, 7/10, 14/10, 21/10,
amphi 1030-12 (Arago), TD 1330-1530, 1545-1745 (SI31,32,33,34)
Books:
(Polycopié), 2006
. Sanders, Algorithms and Data Structures, Springer, 2008 Website: www.enseignement.polytechnique.fr/informatique/INF421 Contact: liberti@lix.polytechnique.fr (e-mail subject: INF421)
INF421, Lecture 2 – p. 2
LinkedList<Integer> L = new LinkedList<Integer>();
INF421, Lecture 2 – p. 3
not going to be easy to make everyone happy. I aim to keep the rhythm slow
but escalate the contents’ difficulty
different people presenting the same material in different ways, you’ll understand it much better. Remember, this is still a foundational course
were ALREADY on the website, you could have printed them yourselves!)
code at the end of each lecture
lively, but there will certainly be times when you get bored. I apologize in advance and hope these will be kept to a minimum
INF421, Lecture 2 – p. 4
INF421, Lecture 2 – p. 5
INF421, Lecture 2 – p. 6
A 1 h:00 2 h:10 3 h:30 B 1 h:00 4 h:20 5 h:40 C 2 h:10 3 h:20 5 h:30 D 4 h:20 5 h:40 6 h:50 E 2 h:05 5 h:10 6 h:30 F 3 h:25 4 h:30 6 h:40 Find a convenient itinerary from 1 to 6, leaving at h:00?
INF421, Lecture 2 – p. 7
red arrows: waiting for next bus
1/00 2/10 A/10 4/20 B/20 2/05 5 5/10 E/5 3/20 C/10 3/30 A/20 3/25 5 5/30 C/10 5 4/30 F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 8
1/00 2/10 A/10 4/20 B/20 2/05 5 5/10 E/5 3/20 C/10 3/30 A/20 3/25 5 5/30 C/10 5 4/30 F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00
2/10 A/10 4/20 B/20 2/05 5 5/10 E/5 3/20 C/10 3/30 A/20 3/25 5 5/30 C/10 5 4/30 F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00
2/10
A/10
4/20
B/20 2/05 5 5/10 E/5 3/20 C/10 3/30 A/20 3/25 5 5/30 C/10 5 4/30 F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00
2/10
A/10 4/20 B/20 2/05 5 5/10 E/5 3/20 C/10 3/30 A/20 3/25 5 5/30 C/10 5 4/30 F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00 2/10 A/10 4/20 B/20 2/05 5 5/10 E/5
3/20
C/10
3/30
A/20 3/25 5 5/30 C/10 5 4/30 F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00 2/10 A/10
4/20
B/20 2/05 5 5/10 E/5 3/20 C/10 3/30 A/20 3/25 5 5/30 C/10 5 4/30 F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00 2/10 A/10 4/20 B/20 2/05 5 5/10 E/5 3/20 C/10 3/30 A/20 3/25 5 5/30 C/10 5
4/30
F/5 1 0
5/40
B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00 2/10 A/10 4/20 B/20 2/05 5 5/10 E/5
3/20
C/10 3/30 A/20 3/25 5 5/30 C/10 5 4/30 F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00 2/10 A/10 4/20 B/20 2/05 5 5/10 E/5 3/20 C/10 3/30 A/20
3/25
5
5/30
C/10 5 4/30 F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00 2/10 A/10 4/20 B/20 2/05 5 5/10 E/5 3/20 C/10
3/30
A/20 3/25 5 5/30 C/10 5 4/30 F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00 2/10 A/10 4/20 B/20 2/05 5 5/10 E/5 3/20 C/10 3/30 A/20 3/25 5 5/30 C/10 5 4/30 F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00 2/10 A/10 4/20 B/20 2/05 5 5/10 E/5 3/20 C/10 3/30 A/20 3/25 5 5/30 C/10 5
4/30
F/5 1 0 5/40 B/20 D/20 6/40 F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
1/00 2/10 A/10 4/20 B/20 2/05 5 5/10 E/5 3/20 C/10 3/30 A/20 3/25 5 5/30 C/10 5 4/30 F/5 1 0 5/40 B/20 D/20
6/40
F/10 2 0 6/30 E/20 1 0 6/50 D/10 1 0 1 0
INF421, Lecture 2 – p. 9
At each iteration, store nodes out of queue with predecessors
INF421, Lecture 2 – p. 10
1/00 2/10 4/20 2/05 5/10 3/20 3/30 3/25 5/30 4/30 5/40 6/40 6/30 6/50 A/10 B/20 5 E/5 C/10 A/20 5 C/10 5 F/5 10 B/20 D/20 F/10 20 E/20 10 D/10 10 10 G/5
3/25 is still in the queue ( 5/40 3/25 5/30 6/40 ) at termination, can’t reach 6/30
INF421, Lecture 2 – p. 11
instead of stopping when the arrival node is found, let τ ∗ be the arrival time of best solution so far let τ ′ be the minimum time of the nodes in the queue
if (τ ′ ≥ τ ∗) then
terminate
endif
INF421, Lecture 2 – p. 12
instead of stopping when the arrival node is found, let τ ∗ be the arrival time of best solution so far let τ ′ be the minimum time of the nodes in the queue
if (τ ′ ≥ τ ∗) then
terminate
endif
INF421, Lecture 2 – p. 12
instead of stopping when the arrival node is found, let τ ∗ be the arrival time of best solution so far let τ ′ be the minimum time of the nodes in the queue
if (τ ′ ≥ τ ∗) then
terminate
endif
where “bus, waiting, bus” counts as two changes, not one
INF421, Lecture 2 – p. 12
instead of stopping when the arrival node is found, let τ ∗ be the arrival time of best solution so far let τ ′ be the minimum time of the nodes in the queue
if (τ ′ ≥ τ ∗) then
terminate
endif
where “bus, waiting, bus” counts as two changes, not one
INF421, Lecture 2 – p. 12
instead of stopping when the arrival node is found, let τ ∗ be the arrival time of best solution so far let τ ′ be the minimum time of the nodes in the queue
if (τ ′ ≥ τ ∗) then
terminate
endif
where “bus, waiting, bus” counts as two changes, not one
INF421, Lecture 2 – p. 12
INF421, Lecture 2 – p. 13
insert an element at the end of the queue retrieve and delete the element at the beginning of the queue test if the queue is empty find the size of the queue
INF421, Lecture 2 – p. 14
insert an element at the end of the queue retrieve and delete the element at the beginning of the queue test if the queue is empty find the size of the queue
INF421, Lecture 2 – p. 14
insert an element at the end of the queue retrieve and delete the element at the beginning of the queue test if the queue is empty find the size of the queue
INF421, Lecture 2 – p. 14
qh = d0 (first element of queue) qt = ⊥ delimits the end of the queue qi is unused for 0 ≤ i < h and t < i ≤ n actual implemen- tation of a circular array is simply an array; but the be- haviour will be dif- ferent
INF421, Lecture 2 – p. 15
INF421, Lecture 2 – p. 16
INF421, Lecture 2 – p. 17
assert(size() < n)
INF421, Lecture 2 – p. 18
t = n
t = 0
INF421, Lecture 2 – p. 19
h = t
INF421, Lecture 2 – p. 20
INF421, Lecture 2 – p. 20
INF421, Lecture 2 – p. 21
Input: set V , binary relation ∼ on V , and s = t ∈ V
INF421, Lecture 2 – p. 22
INF421, Lecture 2 – p. 23
INF421, Lecture 2 – p. 23
INF421, Lecture 2 – p. 23
INF421, Lecture 2 – p. 23
INF421, Lecture 2 – p. 23
INF421, Lecture 2 – p. 23
INF421, Lecture 2 – p. 24
INF421, Lecture 2 – p. 25
INF421, Lecture 2 – p. 26
INF421, Lecture 2 – p. 26
INF421, Lecture 2 – p. 26
INF421, Lecture 2 – p. 26
Remark: the number of changes in an itinerary is the
Idea of proof:
found by BFS shortest
h − 1
ℓ+1<h
ℓ + 1 = h
INF421, Lecture 2 – p. 27
INF421, Lecture 2 – p. 28
WARNING: BFS will not find shortest paths in a
INF421, Lecture 2 – p. 29
INF421, Lecture 2 – p. 30
INF421, Lecture 2 – p. 30
INF421, Lecture 2 – p. 30
INF421, Lecture 2 – p. 30
INF421, Lecture 2 – p. 30
INF421, Lecture 2 – p. 31
u = Q.popFront();
for v ∈ V (v ∼ u ∧ v ∈ L) do
α(v) = α(u) + 1
if v = t then
return α(v);
end if
Q.pushBack(v);
L.pushBack(v);
end for
u = min< Q; Q ← Q {u};
for v ∈ V (v ∼ u ∧ v ∈ L) do
α(v) = α(u) + 1
if v = t then
return α(v);
end if
Q ← Q ∪ {v} (v = max< Q);
L ← L ∪ {v};
end for
INF421, Lecture 2 – p. 32
for v ∈ V (v ∼ u ∧ v ∈ L) do
INF421, Lecture 2 – p. 33
for v ∈ V (v ∼ u ∧ v ∈ L) do
INF421, Lecture 2 – p. 33
for v ∈ V (v ∼ u ∧ v ∈ L) do
with a jagged array, the u-th row can have size at worst O(|V |), if every v is in relation with u if we keep a map A : V × V → {0, 1} such that A(u, v) = 1 whenever u ∼ v and 0 otherwise, we reduce this to O(1)
INF421, Lecture 2 – p. 33
for v ∈ V (v ∼ u ∧ v ∈ L) do
with a jagged array, the u-th row can have size at worst O(|V |), if every v is in relation with u if we keep a map A : V × V → {0, 1} such that A(u, v) = 1 whenever u ∼ v and 0 otherwise, we reduce this to O(1)
INF421, Lecture 2 – p. 33
for v ∈ V (v ∼ u ∧ v ∈ L) do
with a jagged array, the u-th row can have size at worst O(|V |), if every v is in relation with u if we keep a map A : V × V → {0, 1} such that A(u, v) = 1 whenever u ∼ v and 0 otherwise, we reduce this to O(1)
INF421, Lecture 2 – p. 33
for v ∈ V (v ∼ u ∧ v ∈ L) do
with a jagged array, the u-th row can have size at worst O(|V |), if every v is in relation with u if we keep a map A : V × V → {0, 1} such that A(u, v) = 1 whenever u ∼ v and 0 otherwise, we reduce this to O(1)
INF421, Lecture 2 – p. 33
INF421, Lecture 2 – p. 34
INF421, Lecture 2 – p. 34
INF421, Lecture 2 – p. 34
for v ∈ V (v ∼ u ∧ α(v) = |V | + 1) do
INF421, Lecture 2 – p. 34
for v ∈ V (v ∼ u ∧ α(v) = |V | + 1) do
INF421, Lecture 2 – p. 34
for v ∈ V (v ∼ u ∧ α(v) = |V | + 1) do
INF421, Lecture 2 – p. 34
The internal loop
for v ∈ V (v ∼ u ∧ α(v) = |V | + 1) do
INF421, Lecture 2 – p. 35
The internal loop
for v ∈ V (v ∼ u ∧ α(v) = |V | + 1) do
Because u is never considered more than once by Thm. 1, it follows that no pair (u, v) is ever considered more than once over all iterations w.r.t. both loops
INF421, Lecture 2 – p. 35
The internal loop
for v ∈ V (v ∼ u ∧ α(v) = |V | + 1) do
Because u is never considered more than once by Thm. 1, it follows that no pair (u, v) is ever considered more than once over all iterations w.r.t. both loops At worst, the instruction Q.pushBack(v) can only be repeated as many times as there are pairs in the relation ∼
INF421, Lecture 2 – p. 35
The internal loop
for v ∈ V (v ∼ u ∧ α(v) = |V | + 1) do
Because u is never considered more than once by Thm. 1, it follows that no pair (u, v) is ever considered more than once over all iterations w.r.t. both loops At worst, the instruction Q.pushBack(v) can only be repeated as many times as there are pairs in the relation ∼ Moreover, we execute the body of the outer loop at least |V | times
INF421, Lecture 2 – p. 35
The internal loop
for v ∈ V (v ∼ u ∧ α(v) = |V | + 1) do
Because u is never considered more than once by Thm. 1, it follows that no pair (u, v) is ever considered more than once over all iterations w.r.t. both loops At worst, the instruction Q.pushBack(v) can only be repeated as many times as there are pairs in the relation ∼ Moreover, we execute the body of the outer loop at least |V | times Asymptotically, we cannot compare n, m: it depends on the graph
INF421, Lecture 2 – p. 35
The internal loop
for v ∈ V (v ∼ u ∧ α(v) = |V | + 1) do
Because u is never considered more than once by Thm. 1, it follows that no pair (u, v) is ever considered more than once over all iterations w.r.t. both loops At worst, the instruction Q.pushBack(v) can only be repeated as many times as there are pairs in the relation ∼ Moreover, we execute the body of the outer loop at least |V | times Asymptotically, we cannot compare n, m: it depends on the graph ⇒ The worst-case complexity of BFS is then O(|V | + | ∼ |)
INF421, Lecture 2 – p. 35
INF421, Lecture 2 – p. 36
through a maze, which appeared in the proceedings of a
eorie des graphes et ses applications by
INF421, Lecture 2 – p. 37
INF421, Lecture 2 – p. 38
INF421, Lecture 2 – p. 39
INF421, Lecture 2 – p. 40