Important Updates and Algorithm Insights

Lecture 22
CSE 331
Oct 24, 2022
Project deadlines coming up
Group formation instructions
 
F
o
l
l
o
w
 
i
n
s
t
r
u
c
t
i
o
n
s
 
E
X
A
C
T
L
Y
 
a
s
 
t
h
e
y
 
a
r
e
 
s
t
a
t
e
d
Please be in touch w/ your group
1-on-1 meetings
Guest lecture on Wed
A
.
 
E
r
d
e
m
 
S
a
r
ı
y
ü
c
e
My 4pm
OH
canceled
Trevor
will run
late night
OH
One time amnesty for AI violation
Questions/Comments?
Kruskal’
s Algorithm
Joseph B. Kruskal
Input: 
G=(V,E)
, 
c
e
> 0 
for every 
e
 in 
E
T = Ø
Sort edges in increasing order of their cost
Consider edges in sorted order
If an edge can be added to 
T
 without adding a cycle then add it to 
T
Cut Property Lemma for MSTs
S
V \ S
Cheapest crossing edge is in 
all
 MSTs
Condition: 
S
 and 
V\S
 are non-empty
Assumption: All edge costs are distinct
Questions/Comments?
Today’s agenda
Optimality of Kruskal’s algorithm
Remove distinct edge weights assumption
Quick runtime analysis of Prim’s+Kruskal’s
S
V \ S
Optimality of Kruskal’
s Algorithm
Input: 
G=(V,E)
, 
c
e
> 0 
for every 
e
 in 
E
T = Ø
Sort edges in increasing order of their cost
Consider edges in sorted order
If an edge can be added to 
T
 without adding a cycle then add it to 
T
S
Nodes
connected to 
red
in 
(V,T)
 
S
 is non-empty
 
V\S
 is non-empty
 
First crossing edge considered
Is 
(V,T)
 a spanning tree?
 
No cycles by design
 
Just need to show that 
(V,T) 
is connected
No edges here
G
 is
disconnected!
Removing distinct cost assumption
 
Change all edge weights by very small amounts
 
Make sure that all edge weights are distinct
 
MST for “
perturbed” weights is the same as for original
 
Changes have to be small enough so that this holds
EXERCISE: Figure out how to change
costs
Questions/Comments?
Running time for Prim’
s algorithm
Similar to Dijkstra
s algorithm
Input: 
G=(V,E)
, 
c
e
> 0 
for every 
e
 in 
E
S = {s}, T = Ø
While 
S
 is not the same as 
V
Among edges 
e= (u,w)
 with 
u
 in 
S
 and 
w
 not in 
S
, pick one with minimum cost 
Add 
w
 to 
S
,
 e 
to
 T
O(m log n)
Running time for Kruskal’s Algorithm
Joseph B. Kruskal
Input: 
G=(V,E)
, 
c
e
> 0 
for every 
e
 in 
E
T = Ø
Sort edges in increasing order of their cost
Consider edges in sorted order
If an edge can be added to 
T
 without adding a cycle then add it to 
T
Can be verified in 
O(m+n)
 time
O(m
2
) 
time
overall
Reading Assignment
 
Sec 4.5, 4.6 of [KT]
High Level view of the course
Problem Statement
Algorithm
Problem Definition
Implementation
Analysis
Correctness+Runtime Analysis
Data Structures
Three general
techniques
Done with
greedy
Trivia
Divide and Conquer
Divide up the problem into at least two sub-problems
Recursively solve the sub-problems
Patch up
 the solutions to the sub-problems for the final solution
Sorting
Given 
n
 numbers order them from smallest to largest
Works for any set of elements on which there is a total order
Insertion Sort
Input: 
a
1
, 
a
2
,…., 
a
n
Make sure that all the
processed numbers
are sorted
 
Output: 
b
1
,
b
2
,…,
b
n
O(log n)
O(n)
O(n
2
) 
overall
Other 
O(n
2
) 
sorting algorithms
Selection Sort: 
In every round pick the min among remaining numbers
Bubble sort: 
The smallest number “
bubbles” up
Divide and Conquer
Divide up the problem into at least two sub-problems
Recursively solve the sub-problems
“Patch up” the solutions to the sub-problems for the final solution
Mergesort Algorithm
Divide up the numbers in the middle
Sort each half recursively
Merge the two sorted halves into one sorted output
Unless 
n=2
How fast can sorted arrays be merged?
Group talk time
Mergesort algorithm
Input: 
a
1
, a
2
, …, a
n
Output: Numbers in sorted order
If 
n = 1 
return
 the order 
a
1
An example run
If 
n = 1 
return
 the order 
a
1
Correctness
Input: 
a
1
, a
2
, …, a
n
Output: Numbers in sorted order
By
induction
on 
n
Inductive step follows from correctness of MERGE
If 
n = 1 
return
 the order 
a
1
 
Runtime analysis on the board…
Slide Note
Embed
Share

Stay informed with the latest project deadlines, group formation instructions, and upcoming 1-on-1 meetings. Dive into essential topics like Kruskal's Algorithm, Cut Property Lemma for MSTs, and the optimality of Kruskal's Algorithm. Get ready to enhance your understanding of algorithmic concepts and their applications.

  • Updates
  • Project Deadlines
  • Algorithm Insights
  • Kruskals Algorithm
  • Cut Property

Uploaded on Feb 20, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Lecture 22 CSE 331 Oct 24, 2022

  2. Project deadlines coming up

  3. Group formation instructions Follow instructions EXACTLY as they are stated

  4. Please be in touch w/ your group

  5. 1-on-1 meetings

  6. Guest lecture on Wed Trevor will run late night OH My 4pm OH canceled A. Erdem Sar y ce

  7. One time amnesty for AI violation

  8. Questions/Comments?

  9. Kruskals Algorithm Input: G=(V,E), ce> 0 for every e in E T = Sort edges in increasing order of their cost Joseph B. Kruskal Consider edges in sorted order If an edge can be added to T without adding a cycle then add it to T

  10. Cut Property Lemma for MSTs Condition: S and V\S are non-empty S V \ S Cheapest crossing edge is in all MSTs Assumption: All edge costs are distinct

  11. Questions/Comments?

  12. Todays agenda Optimality of Kruskal s algorithm Remove distinct edge weights assumption Quick runtime analysis of Prim s+Kruskal s

  13. Optimality of Kruskals Algorithm Nodes V \ S S connected to red in (V,T) S Input: G=(V,E), ce> 0 for every e in E S is non-empty T = V\S is non-empty Sort edges in increasing order of their cost First crossing edge considered Consider edges in sorted order If an edge can be added to T without adding a cycle then add it to T

  14. Is (V,T) a spanning tree? No cycles by design G is disconnected! Just need to show that (V,T) is connected V \ S S No edges here

  15. Removing distinct cost assumption Change all edge weights by very small amounts Make sure that all edge weights are distinct MST for perturbed weights is the same as for original Changes have to be small enough so that this holds EXERCISE: Figure out how to change costs

  16. Questions/Comments?

  17. Running time for Prims algorithm Similar to Dijkstra s algorithm O(m log n) Input: G=(V,E), ce> 0 for every e in E S = {s}, T = While S is not the same as V Among edges e= (u,w) with u in S and w not in S, pick one with minimum cost Add w to S, e to T

  18. Running time for Kruskals Algorithm Can be implemented in O(m log n) time (Union-find DS) Input: G=(V,E), ce> 0 for every e in E O(m2) time overall T = Sort edges in increasing order of their cost Joseph B. Kruskal Consider edges in sorted order If an edge can be added to T without adding a cycle then add it to T Can be verified in O(m+n) time

  19. Reading Assignment Sec 4.5, 4.6 of [KT]

  20. High Level view of the course Problem Statement Problem Definition Done with greedy Three general techniques Algorithm Implementation Data Structures Analysis Correctness+Runtime Analysis

  21. Trivia

  22. Divide and Conquer Divide up the problem into at least two sub-problems Recursively solve the sub-problems Patch up the solutions to the sub-problems for the final solution

  23. Sorting Given n numbers order them from smallest to largest Works for any set of elements on which there is a total order

  24. Insertion Sort Make sure that all the processed numbers are sorted Input: a1, a2, ., an Output: b1,b2, ,bn O(n2) overall b1= a1 for i =2 n Find 1 j i s.t. ai lies between bj-1 and bj Move bj to bi-1 one cell down a b 4 4 2 3 1 bj=ai O(log n) 3 3 4 2 2 4 3 1 4 O(n)

  25. Other O(n2) sorting algorithms Selection Sort: In every round pick the min among remaining numbers Bubble sort: The smallest number bubbles up

  26. Divide and Conquer Divide up the problem into at least two sub-problems Recursively solve the sub-problems Patch up the solutions to the sub-problems for the final solution

  27. Mergesort Algorithm Divide up the numbers in the middle Unless n=2 Sort each half recursively Merge the two sorted halves into one sorted output

  28. How fast can sorted arrays be merged? Group talk time

  29. Mergesort algorithm Input: a1, a2, , an Output: Numbers in sorted order MergeSort( a, n ) If n = 1 return the order a1 If n = 2 return the order min(a1,a2); max(a1,a2) aL = a1, , an/2 aR = an/2+1, , an return MERGE ( MergeSort(aL, n/2), MergeSort(aR, n/2) )

  30. An example run 51 1 100 19 2 8 4 3 1 51 19 100 2 8 3 4 1 19 51 100 2 3 4 8 8 19 51 100 1 2 3 4 MergeSort( a, n ) If n = 1 return the order a1 If n = 2 return the order min(a1,a2); max(a1,a2) aL = a1, , an/2 aR = an/2+1, , an return MERGE ( MergeSort(aL, n/2), MergeSort(aR, n/2) )

  31. Correctness Input: a1, a2, , an Output: Numbers in sorted order MergeSort( a, n ) By induction on n If n = 1 return the order a1 If n = 2 return the order min(a1,a2); max(a1,a2) aL = a1, , an/2 aR = an/2+1, , an return MERGE ( MergeSort(aL, n/2), MergeSort(aR, n/2) ) Inductive step follows from correctness of MERGE

  32. Runtime analysis on the board

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#