/*

Theory HW #4
Data Structures - Shlomo
Ramzi Abdoch - raa2148
*/
// Number 1
initial:
processA:
processC:
processB:
processG:
processE:
processF:
processD:

0
0
0
0
0
0
0
0

|
|
|
|
|
|
|
|

A
|
|
|
|
|
|
|
|


5
5
5
5
5
5
5

|

3
3
3
3
3
3
3

B
|
|
|
|
|
|
|
|

|


10
10
10
9
9
9

C
|
|
|
|
|
|
|
|

|


10
8
7
7
7
7

D
|
|
|
|
|
|
|
|

|





8
8
8

E
|
|
|
|
|
|
|
|

| F | G



6
6
6
6
6

Work:
A's
C's
B's
the
G's
E's
F's
D's

neighbors
neighbors
neighbors
same
neighbors
neighbors
neighbors
neighbors

= B, C; C's closer.
= E, D; (A,E) = A->C->E = 10; (A,D) = A->C->D = 10
= G, E, C; (A,G) = A->B->G = 6; (A,E)* = A->B->E = 8; (A,C) stays
=
=
=
=

E; (A, E)* = 7
F, D; (A, F) = 8, (A,D)* = 9
{}
F; (A,F) stays the same

*updates
// Number 2
initial:
processB:
processC:
processE:
processG:
processD:
processF:
processA:






3
3
3

|
|
|
|
|
|
|
|

0
0
0
0
0
0
0
0

A
|
|
|
|
|
|
|
|

|

1
1
1
1
1
1
1

B
|
|
|
|
|
|
|
|

|


2
2
2
2
2
2

C
|
|
|
|
|
|
|
|

|

1
1
1
1
1
1
1

D
|
|
|
|
|
|
|
|

|



2
2
2
2
2

E
|
|
|
|
|
|
|
|

| F | G

1
1
1
1
1
1
1

// Number 3
When adding a vertex, one must simply add the vertex to the array of vertices, t
hen build the linked list of its adjacencies, taking O(1) time. When deleting a
vertex, one must traverse the vertex array and each linked list to see if the ve
rtex is present, then remove it. All edges will be seen, hence O(m) runtime.
// Number 4
a)
i) union(1,2)
/ | / | 1 | / | / | / | / | / | / | / | / | / | / | / | / | / | /
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16
ii) union(3,4)

5) / | / | 1 | / | 3 | 3 | / | / | / | / | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 iv) union(1.13) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xii) union(14.16) .7) / | / | 1 | / | 3 | 3 | / | 1 | / | / | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 v) union(3.9) / | / | 1 | / | 3 | 3 | 3 | 1 | / | 8 | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 vii) union(1.11) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 x) union(3.10) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 ix) union(3.15) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | / | 14 | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xiii) union(16.12) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xi) union(3./ | / | 1 | / | 3 | / | / | / | / | / | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 iii) union(3.8) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 viii) union(3.6) / | / | 1 | / | 3 | 3 | 3 | 1 | / | / | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 vi) union(8.0) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | / | 14 | 0 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xiv) union(14.

5) / | / | 1 | / | 3 | 3 | / | / | / | / | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 iv) union (1.6) / | / | 1 | / | 3 | 3 | 3 | 1 | / | / | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 vi) union (8.3) 14 | / | 1 | 1 | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | / | 14 | 14 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xvi) union(1.7) / | / | 1 | / | 3 | 3 | / | 1 | / | / | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 v) union (3.10) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 ix) union (3.2) / | / | 1 | / | / | / | / | / | / | / | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 ii) union (3.11) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 .4) / | / | 1 | / | 3 | / | / | / | / | / | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 iii) union (3.14 | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | / | 14 | 0 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xv) union(1.14) 14 | / | 1 | 1 | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | 1 | 14 | 14 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 b) i) union (1.8) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 viii) union (3.9) / | / | 1 | / | 3 | 3 | 3 | 1 | / | 8 | / | / | / | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 vii) union (1.

With mergesort. which is different than how I'd do it by hand because I can only pay att ention to a few piles at once. I like mergesort because all the division takes place at the beg inning.12) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | / | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xi) union (3.15) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | / | 14 | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xiii) union (16. etc. etc.3) / | 3 | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | 0 | 14 | 0 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xv) union (1. Sort the piles. then make two piles from the other pile.0) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | / | 14 | 0 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xiv) union (14. voila. then we rebuild the list from those single units. choosing a pivot at random and using the two desks to put the high/low piles on. // Number 7 GRAPH ATTACHED AS FILE: Graph_Theory4_7. it's like mergesort but you simply make two big piles. merge them. With quick sort.14) 3 | 3 | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | 0 | 14 | 0 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 c) // Number 5 Execute a quicksort-like algorithm. // Number 6 So.x) union (3. I think the difference is simply in how much division is actually taking pla ce.C): .16) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | 0 | 14 | 0 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xv) union (1.jpg process(B.13) / | / | 1 | / | 3 | 3 | 3 | 1 | 1 | 8 | 3 | 3 | 3 | 3 | / | / | / 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 xii) union (14. the data is being divided until it is a single unit. sort one.

Eq. Classes: F-A-B-C-E.G): Eq. G. E. D process(A.D): Eq. H. // Number 8 MST) Using: Kruskal's Algorithm i) 5-3 ii) 5-3. Classes: F-A-B-C. D. 4-2 iv) 5-3. H. D \ H-G process(F. F. 4-2-8-1 v) 3-5-4-2-8-1 vi) 3-5-4-2-8-1. H. Classes: B-C. H-G.F): Eq. process(H.E): Eq.H): Eq.E): NO CHANGE process(F. Classes: F-A-B-C. Classes: F-A-B-C.G): NO CHANGE process(G. G. A. G. E. Classes: B-C. 8-1 iii) 5-3. D \ H-G process(F. D process(A. D process(A. E.H): NO CHANGE process(C. Classes: F-A-B-C-E \ H-G-D DONE. E. E. A-F.B): Eq. // Number 9 . 8-1. 7-6 vii) 3-5-4-2-8-1 \ 6-7 DONE.

GRAPH ATTACHED AS FILE: Graph_Theory4_9.jpg .

Sign up to vote on this title
UsefulNot useful