Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

B-Tree of order 4

Each node has at most 4 pointers and 3 keys, and at least 2 pointers and 1 key.
Insert: 5, 3, 21, 9, 1, 13, 2, 7, 10, 12, 4, 8

m=4 Create node. Max key = k


1 k=m-1
=4-1
=3

Insert values to be
2 inserted in an ascending
fashion

Insert: 5, 3, 21, 9, 1, 13, 2, 7, 10, 12, 4, 8

3 5 9 21

More than the


3
key, split node

3 9 21

Continue until nodes are more


4
than the key, then split again

1, 13, 2, 7, 10, 12, 4, 8

if < 5 if > 5

1 3 9 21

5 5

1 3 9 13 21

6 5

1 2 3 9 13 21
7 5

1 2 3 7 9 13 21

split!

8 5 9
if < 5 if > 9

1 2 3 7 13 21

8 5 9

1 2 3 7 10 13 21

8 5 9

1 2 3 7 10 12 13 21

split!

8 5 9 12
if < 5 if > 12

1 2 3 7 10 13 21

8 5 9 12

1 2 3 4 7 10 13 21

split!
8 2 5 9 12

split!

1 3 4 7 10 13 21

8 5
if < 5 if > 5

2 9 12

if < 2 if > 2 if < 9 if > 12

1 3 4 7 10 13 21

8 5

2 9 12

1 3 4 7 8 10 13 21

8 B-tree Complete 5

2 9 12

1 3 4 7 8 10 13 21
Graphs

1. Construct the following Graph Representation from given Directed Graph above.
Adjacency Matrix
Adjacency List
2. Show the Depth-first and Breadth-first traversal
3. Identify all valid path sequence. Find the shortest path sequence through sum of the weights (costs) of the edges involved in the path.

1.a Adjacency Matrix


1

n=6 0 1 2 3 4 5 Given: B

0 0 10 5 15 0 0 10 10 30

1 0 0 0 0 0 30 0 A 15 D 20 F 5
3
2 0 0 0 10 3 0 5 10 15

3 0 10 0 0 0 20 C 3 E

2 4
4 0 0 0 0 0 15

5 0 0 0 0 0 0

1.b Adjacency List


"index" "data" "weight"

0 A 1 B 10 2 C 5 3 D 15 null

1 B 5 F 30 null

2 C 3 D 10 4 E 3 null

3 D 1 B 10 5 F 20 null

4 E 5 F 15 null

5 F null
2.a Depth-First Traversal

B
1 2

A D F B F
D D D
A C C C C E

Popped: A B F D C E
4

C E

DFS: A B F D C E
Dequeued:

2.b Breadth-First Traversal

B
4 A
1

A B D C

2
B D C F

A D F
D C F E

C F E

3
F E

C E
E

5
BFS: A B D C F E

3 Paths
B a A → B →F 10 + 30 = 40

10 10 30 b A→D→B→F 15 + 10 + 30 = 55

c A→D→F 15 + 20 = 35
A 15 D 20 F
d A→C→D→B→F 5 + 10 + 10 + 30 = 55

5 10 15
e A→C→D→F 5 + 10 + 20 = 35

C 3 E f A→C→E→F 5 + 3 + 15 = 23

From A to F, the shortest path would be path f ( A → C → E → F ), costing 23

You might also like