Professional Documents
Culture Documents
Lec12 Graph Heap
Lec12 Graph Heap
Objective:
To introduce the properties of a graph and
heap
To explain the graph traversal and basic
algorithm
To explain the heap basic algorithm
1
GRAPH
2
Graph Definition
vertices
A B
lines
B C D
E F H G I
E
G P M J
P Y
H E E M M J
A X G G G G G G G
1. if (empty graph)
1.1 return
2. walkPtr = graph.first
3. loop (walkPtr)
3.1 walkPtr->processed = 0
3.2 walkPtr = walkPtr->nextVertex
4. end loop
5. createStack (stack)
6. walkPtr = graph.first
7. loop (walkPtr not null)
7.1 if (walkPtr->processed < 2)
7.1.1 if (walkPtr->processed < 1)
7.1.1.1 pushStack (stack, walkPtr)
7.1.1.2 walkPtr->processed = 1
7.1.2 end if
Struktur Data & Algoritma
Cont.
7.1.3 loop (not emptyStack(stack))
7.1.3.1 popStack (stack, vertexPtr)
7.1.3.2 process (vertexPtr->dataPtr)
7.1.3.3 vertexPtr->processed = 2
7.1.3.4 arcWalkPtr = vertexPtr->arc
7.1.3.5 loop (arcWalkPtr not null)
i. vertToPtr = arcWalkPtr->destination
ii. if (vertToPtr->processed is 0)
a. pushStack (stack, vertToPtr)
b. vertToPtr->processed = 1
iii. end if
iv. arcWalkPtr = arcWalkPtr->nextArc
7.1.3.6 end loop
7.1.4 end loop
7.2 end if
7.3 walkPtr = walkPtr->nextVertex
8. end loop
9. destroyStack (stack)
10. return
End depthFirst
Struktur Data & Algoritma
Breadth First Traversal
B C D
E F H G I
E
G P M J
A X GH HP PE E MY YJ J
1. if (empty graph)
1.1 return
2. end if
3. createQueue (queue)
4. walkPtr = graph.first
5. loop (walkPtr not null)
5.1 walkPtr->processed = 0
5.2 walkPtr = walkPtr->nextVertex
6. end loop
7. walkPtr = graph.first
8. loop (walkPtr not null)
8.1 if (walkPtr->processed < 2)
8.1.1 if (walkPtr->processed < 1)
i. enqueue (queue, walkPtr)
ii. walkPtr->processed = 1
8.1.2 end if
Struktur Data & Algoritma
Cont……
8.1.3 loop (not emptyQueue (queue))
i. dequeue (queue, vertexPtr)
ii. process (vertexPtr)
iii. vertexPtr->processed = 2
iv. arcPtr = vertexPtr->arc
v. loop (arcPtr not null)
a. toPtr = arcPtr->destination
b. if (toPtr->processed zero)
1. enqueue (queue, toPtr)
2. toPtr->processed = 1
c. end if
d. arcPtr = arcPtr->nextArc
vi. end loop
8.1.4 end loop
8.2 end if
8.3 walkPtr = walkPtr->nextVertex
9. end loop
10. destroyQueue (queue)
11. return
End breadthFirst
Struktur Data & Algoritma
Graph operation
28
What is heap ?
the min-heap property: the value the max-heap property: the value
of each node is greater than or of each node is less than or equal
equal to the value of its parent, to the value of its parent, with the
with the minimum-value element maximum-value element at the
at the root. root.
1 2
4 5 6
3
7 8
9
If node x is in location i
left_child(x) -------------> 2i + 1
0 1 2 3 4 5 6 7 8 9 right_child(x) -----------> 2i + 2
parent(x) ----------------> [(i -1)/ 2]
16 12 20 30
13 15 10 25
Struktur Data & Algoritma
ReheapUp
42
21 32
Last element (25)
Moved up
16 25 20 30
13 15 10 12
42
25 32 Moved up again
Tree now is a heap
16 21 20 30
13 15 10 12
17 19 36 7
25 70
Struktur Data & Algoritma
2
3 10 Root moved down
(right)
17 19 36 7
25 70
2
3 7
Moved down again
Tree now is a heap
17 19 36 10
25 70