

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The solutions to assignment 9 of a computer science course focusing on various graph algorithms, including depth-first, breadth-first, prim's, kruskal's, and dijkstra's algorithms. The solutions include recursive call stacks, priority queues, and sorted lists.
Typology: Assignments
1 / 2
This page cannot be seen from the preview
Don't miss anything!
Recursive depth-first
From A recursive call stack shown
A to B AB
B to C AB BC
C to D AB BC CD
D to G AB BC CD DG
G to E AB BC CD DB GE
E to F AB BC CD DB GE EF
Breadth-first
From A (queue) AB AC AD AE AG
A to B AC AD AE AG BC BE...
A to C AD AE AG BC BE CD CF...
A to D AE AG BC BE CD CF...
A to E AG BC BE CD CF...
A to G BC BE CD CF...
Drop BC BE CD CF...
Drop BE CD CF...
Drop CD CF...
C to F_... (discards)_
Prim's algorithm — Note that using a sorted list may select a different set of edges.
From A: AE(1) | AG(1) AD(2) | AC(2) AB(2)
A to E (1): AG(1) | EB(1) EF(1) | AC(2) AB(2) AD(2) EG(2) |
A to G (1): EB(1) | GF(1) EF(1) | EG(2) AB(2) AD(2) GD(1) | AC(2)
E to B (1): GF(1) | BC(1) EF(1) | AC(2) AB(2) AD(2) GD(1) | EG(2)
G to F (1): BC(1) | FC(1) EF(1) | EG(2) AB(2) AD(2) GD(1) | AC(2)
B to C (1): FC(1) | CD(1) EF(1) | AC(2) AB(2) AD(2) GD(1) | EG(2)
Discard FC: CD(1) | EG(2) EF(1) | AC(2) AB(2) AD(2) GD(1)
C to D (1): GD(1) | EG(2) EF(1) | AC(2) AB(2) AD(2)
Six edges in use; terminate run
Kruskal's Algorithm for MST:
Accept AE1 AG1 BC1 BE1 CD1 CF1 --- this bring us to 6 edges
Prim’s Algorithm --- sorted list priority queue, insertion from the
right, use of distance list.
Enter at A, initial priority queue: AE1 AG1 AB2 AC2 AD
Distance list: A: 0 ; B: -> 2 ; C: -> 2 ; D: -> 2 ; E: -> 1 ; F: -> ;
Edge AE1: enter EB1 after AG1, enter EF1 after EB1, discard EG2 (worse)
Distance list: A: 0 ; B: -> 1 ; C: -> 2 ; D: -> 2 ; E: -> 1 ; F: -> 1 ;
Edge AG1: enter GD1 after EF1, discard GF1 (same)
Distance list: A: 0 ; B: -> 1 ; C: -> 2 ; D: -> 1 ; E: -> 1 ; F: -> 1 ;
Edge EB1: enter BC1 after GD
Distance list: A: 0 ; B: -> 1 ; C: -> 1 ; D: -> 1 ; E: -> 1 ; F: -> 1 ;
G: -> 1 — no more changes
Edge EF1: discard FC1 (same)
Edge GD1: discard DC1 (same)
Edge BC1: final edge
Dijkstra's algorithm
Using a sorted list may select a different set of edges, depending on the list protocol.
From A: AE(1) | AG(1) AD(2) | AC(2) AB(2)
A to E (1): AG(1) | AB(2) AD(2) | AC(2) EF(2) omit EB(2=2)
A to G (1): EF(2) | AB(2) AD(2) | AC(2) omit GD(2=2) GF(2=2)
E to F (2): AC(2) | AB(2) AD(2) |
A to C (2): AD(2) | AB(2) omit CD(3>2)
A to D (2): AB(2) |
A to B (2):
Dijkstra’s Algorithm --- sorted list priority queue, insert from the right.
Enter at A, initial priority queue: AE1 AG1 AB2 AC2 AD
Distance list: A: 0 ; B: -> 2 ; C: -> 2 ; D: -> 2 ; E: -> 1 ; F: -> ;
Edge AE1: enter EF2 after AD2, discard EB2 (same), discard EG3 (worse)
Distance list: A: 0 ; B: -> 2 ; C: -> 2 ; D: -> 2 ; E: -> 1 ; F: -> 2 ;
G: -> 1 — no more changes
Edge AG1: discard GD2 (same), discard GF2 (same)
Edge AB2: discard BC3 (worse)
Edge AC2: discard CD3 (worse), discard CF3 (worse)
Edge AD2: all adjacent have been visited
Edge EF2: final edge