Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

/*

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)

/ | / | 1 | / | 3 | / | / | / | / | / | / | / | / | / | / | / | /
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16
iii) union(3,5)
/ | / | 1 | / | 3 | 3 | / | / | / | / | / | / | / | / | / | / | /
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16
iv) 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,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,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,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,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,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,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,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,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,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,16)

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

Eq. Classes: B-C; A; F; E; G; H; D


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

GRAPH ATTACHED AS FILE: Graph_Theory4_9.jpg

You might also like