Академический Документы
Профессиональный Документы
Культура Документы
G1
1
G2
2
2
V1 = {1, 2}
E1 = {(1,1), (1,2)}
V2 = {1, 2, 3, 4, 5}
E2 = {(2,1), (2,3), (2,4), (2,5)}
Directed graph (digraph) is a graph whose edges are ordered pairs. This imposes a directionality to
each edge. We will use sGd to means the edge (s,d) is in the edge set of G. We say that d is adjacent
to s. The set of all vertices adjacent to s is called the adjacency set of s.
Properties of Digraphs
Reflexivity: G is reflexive iff xGx for all vertices x in V. The G I refleive iff every vertex in
G has a self loop.
Antisymmetry: G is antisymmetric iff whenever xGy and yGx then x=y.No two distinct vertices have
edges in both direction.
Transitivity: G is transitive iff for each triple of vertices whenever xGy and yGz then xGz
b
a
Paths
A path is a sequence of edges such that the destination of one edge is the source of the next. The path
is simple if all its vertices except possibly the first and lst are distinct. Length of the path is the
number of edges in it. If there is a path from x to y then y is reachable from x.
A single edge <x,y> is a path of length 1. A self loop <x,x> is a path of lenth 1.
1
2
Strongly connected
Connected
Cycles: A cycle is a path such that the vertex at the destination of the last edge is the source of the first
edge. A digraph is acyclic iff it has no cycles in it.
In-degree of a vertex is the number of edges arriving at that vertex
Out-degree of a vertex is the number of edges leaving that vertex
G
b
One way to represent a digraph G with K vertices is by KxK boolean matrix G' called adjancey matrix
where
G' (x,y) =1 iff xGy
G'=
G' (x,y)=0 iff not xGy
row x indicates the adjacency set of vertex x.
Example:
1
2
1
5
4
3
4
3
5
6
6
Graph Traversals
Some applications require the graph to be traversed . This means that starting from some designated
vertex the graph is walked around in a symmetric way such that vertex is visited only once. Two
algorithms are commonly used:
1. Depth-First Search
2. Breadth-First Search
DEPTH-FIRST SEARCH: Finds all graph vertices reachable from a starting vertex in a way that
explores a given path from the starting vertex before starting another path. Strategy: Go deeper
along a path.
Algorithm:
1. Place the starting vertex (node) x in the set VISITED
2. Do whatever application dependent work is necessary to be done by visiting a vertex (node)
3. For each vertex y adjacent to x, if y has not been visited, call the depth-first search algorithm
recursively with y as the starting vertex (node)
Digraph G
7
Assuming that as an application dependent work we will display the vertex index, if we appply Depthfirst search on G:
Starting at 1 results: 1
2
5
7
3
6
4
Starting at 2 results: 2
5
7
Starting at 3 results: 3
1
2
5
7
4
6
BREADTH-FIRST SEARCH: Visits all vertices adjacent to the starting vertex, then visits all
vertices adjacent to those vertices and so on. The search is broad rather than deep.
Algorithm:
1. Clear the queue Q.
2. Place the starting vertex in the set VISITED
3. Enqueue x in Q
4. Do whatever application dependent work is necessary to be done by visiting a vertex (node)
5. Repeat remaining steps as long as Q is not empty
a. Dequeue a value y from Q
TREES
The notes only cover the basic definitions.
A tree is a special case of a digraph used to express hierarchical relationships or multiway decisions in
computing. For example game tree, company structure, family tree, language tree, expression tree,
sorting and searching trees etc.
Definitions
The common characterisitics of all these trees is that there is a single vertex (the root) that can be
identified as the top of the tree and from the root to any other vertex in the tree there is exactly one
path.
Formally, we can say that tree is a connected digraph such that,
1. There is exactly one vertex called root with in-degree =0
2. All other vertices have in-degree =1
In a general tree there is no restriction about the out-degree and the vertex set may be infinite.
In most application of trees vertex set is finite and the vertices at the bottom have out-degree=0. These
vertices are called leaves or terminals.
A vertex which is neither root nor leaf is called a nonterminal vertex (node).
Every vertex at the destination end of an edge for which the root is the source is itself the root of
another smaller tree called a sub-tree.
The depth of a tree is the length of the longest path from the root.
The destination vertices are called children and the root of it is called parent. Children of the same
parent are called siblings.
Vertex (node) n1 is an ancestor of n2 (and n2 is a descendant of n1) if n1 is either the parent of n2 or
the parent of some ancestor of n2.
A node n2 is a left descendant of node n1 if n2 is either the left child of n1 or a descendant of the left
child of n1. A right descendant may be similarly defined.
Binary Trees
A binary tree is a tree all of whose vertices have out-degree = 2. The subtrees of a binary tree are
ordered in the sense that there is a left child and a right child. If a vertex has only one child it should
Complete Binary Tree: T is a complete binary tree of depth K iff each vertex of level K is a leaf and
each vertex of level less than K has non empty left and right children.
A complete binary tree depth K always has 2 k+1 -1 vertices.
Depth 0 -- single vertex
Depth 1 -- 3 vertices
Depth 2 -- 7 vertices
Almost complete: All K-1 level vertices are complete but level K vertices are missing (useful in some
sorting algorithms such as heap sort).
Balanced: T is balanced iff for each vertex t in T, the depth of t's left and right subtrees differ by at
most one.
1. A binary tree consisting of a single vertex is balanced.
2. A vertex with a single subtree is balanced iff that subtree is a leaf.
3. A binary tree is balanced iff, its leaf and right subtrees are balanced and their depth differ by only
one.
E
W
R
M
A
T
E
G
C
S
E
Lemma: If a complete binary tree with n nodes (depth log 2 n + 1) is represented sequentially. Then
any node with index i 1 i n we have
1. parent(i) is at i/2, if i 1,
( when i=1, the root has no parent. )
2. leftChild(i) is at 2i, if 2i n,
(no left child, if 2i > n.)
lchild
rchild
HEAPSORT
Binary heap is a completely balanced binary tree satisfying the heap property.
Heap property: Given a binary heap A, A[PARENT(i)]A[i]
1
26
24
11
4
5
7
6
8
8
9
1
10
5
10
26
24
11
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
HEAPIFY(A, i)
L LEFT(i)
r RIGHT(i)
if lheap-size[A] and A[l]>A[i]
then largest l
else largest r
if r heap-size[A] and A[r]>A[largest]
then largest r
if largest i
then exchange A[i]] A[largest]
HEAPIFY(A, largest)
1
26
2
i
11
4
5
24
6
8
4
1
8
9
1
10
26
24
4
11
6
8
8
9
1
10
5
1
26
24
11
4
5
7
6
8
8
9
1
1.
2.
3.
10
5
BUILD-HEAP(A)
heap-size[A] length[A]
for i length[A]/2 downto 1
do HEAPIFY(A, i)
h=0
Example Run:
1
3
5
1
26
11
5
4
8
24
10
9
7
5
26
i
5
26
8
1
2
i
11
11
5
24
26
8
1
i
3
10
11
10
24
5
4
10
24
26
11
1
5
24
6
6
26
24
8
1
10
9
7
11
5
8
1
1.
2.
3.
4.
5.
10
9
3
HEAPSORT(A)
BUILD-HEAP(A)
for i length[A] downto 2
do exchange A[1] A[i]
heap-size[A] heap-size[A] 1
HEAPIFY(A, 1)
=
Running time of BUILD-HEAP +
n (loop at step 2.) times running time of HEAPIFY
= O(n) + O(nlgn)
= O(nlgn)
1
Example Run:
2
6
2
2
4
3
1
1
5
7
10
2
4
7
4
5
5
2
4
6
9
1
1
10
2
4
2
6
1
8
4
4
5
4
5
6
7
1
1
10
2
6
6
1
2
4
8
1
1
2
4
2
4
2
6
10
10
1
1
2
6
1
1
1
1
5
5
2
4
10
2
6
7
8
10
2
6
10
11
24
26
BINARY TREE TRAVERSAL: Many applications require traversing a tree so that all the nodes are
visited in a certain order. Traversal algorithms are useful in dealing with binary trees. Three different
forms of traversal is possible: preorder, postorder, inorder traversal.
Preorder Traversal: (node-left-right) (depth-first search)
void traverse-nlr(t-node) {
if (t-node does not have a child ) return;
else {
visittree(t-node);
traverse-nlr( t-node.lchild);
traverse-nlr(t-node.rchild);
}
1
PREORDER TRAVERSAL: 1 2 4 7 5 3 6 8 9
Operations on a BST:
1. Traversing a BST
2. Inserting a record in a BST
3. Finding a record in a BST
4. Deleting a record from a BST
1. If a binary search tree is traversed in inorder and the contents of each node are printed as the node is
visited , the numbers are printed in ascending order.
2. A Recursive Algorithm for insertion:
Assuming that tree is not empty, we first test the new key against the root key: if it is less than we
insert it in the left subtree, if it is equal to or greater insert it in the right subtree.
After several recursive calls we will reach a point where the subtree into which the key to be
inserted is empty, at this point we create a new node and link it to the appropriate pointer in the
parent node.
Successor of 2: 4
Predecessor of 6: 5
6
6
4
If the threads facilitate inorder traversal then the tree is called right-in threaded.
Where are the threads generated? If a node has a right child then its LNR successor is below it
somewhere in the right subtree. Otherwise its LNR successor is above in the tree. Since a node needs
a thread only if it has no right child, common practice is to store the thread in the right child field of
the node. A flag is also needed to indicate if it is a thread or not.
thread
7
D
successor
successor
EXPRESSION TREES
Another application of binary trees in interpreters and compilers. The statement is converted into tree.
For simplicity we will see expression trees. Expression tree is a tree that has operator as its root and
identifiers or constants as its leaves.
A node representing an operator is a nonleaf whereas a node representing an operand is a leaf.
Traversing an expression three in preorder gives its prefix form, traversing it in inorder gives infix
form and traversing in postorder produces postfix form.
+
/
sqrt
8
AVL TREES
BSTs carry a high risk of becoming unbalanced, resulting in expensive search and update
operations.
Idea: Maintain a binary search tree that is almost completely balanced
AVL Trees are BSTs satisfying AVL tree balance:
For every node, the heights of its left and right subtrees differ at most 1.
Depth of an AVL tree with N nodes is at most O(lgN)
Insertion
After insertion, AVL tree might become unbalanced by a difference of at most 2 in the
subtrees.
For the bottommost unbalanced node, S, there are 4 cases:
1. The extra node is the left child of the left child of S.
2. The extra node is the right child of the left child of S.
3. The extra node is the left child of the right child of S.
4. The extra node is the right child of the right child of S.
unbalaced
37
24
37
nodes
42
32
40
24
42
120
42
32
40
42
120
2
5
C
A
Double Rotation
G
S
P
D
C
B
G
C
A
B