Data Structure Unit II
Data Structure Unit II
AND
ALGORITHMS
Unit II Syllabus & Study Material
• Trees • Traversals, Connected
• Binary tree Components and
representations Spanning Trees
• Tree Traversal • Shortest Paths and
• Threaded Binary Trees Transitive closure
• Binary Tree • Activity Networks
Representation of Trees • Topological Sort and
• Graphs and Critical Paths.
Representations
Trees
Trees
Root
Branches
Leaves
Tree - Definition
A tree is a finite set of one
or more nodes such that:
(i)there is a specially
designated node called the
root
(ii)the remaining nodes are
partitioned into n≥0 disjoint
sets T1, ...,Tn where each of
these sets is a tree. T1, ...,Tn
are called the subtrees of
the root.
Tree – Example-Corporate Tree
PARENT=i/2=4/2=2
i=4
LCHILD=2 * i
=2 *4
=8
RCHILD=2 * i+1
= 2 *4+1
=9
BINARY TREE REPRESENTATIONS
• Array Representation
BINARY TREE REPRESENTATIONS
• Linked List Representation
BINARY TREE REPRESENTATIONS
• Linked List Representation
Binary Tree
Traversal
BINARY TREE TRAVERSAL
• There are many operations that are performed on trees.
• One operation is traversing a tree or visiting each node
in the tree exactly once.
L - moving left
D - printing the data
R - moving right
• there are six possible combinations
of traversal: LDR, LRD, DLR, DRL, RDL, and RLD.
• only three traversals LDR, LRD and DLR
NOTATIONS
• Infix Notation
Ex: A + B
• Prefix Notation
Ex: + A B
• Postfix Notation
Ex: A B +
Arithmetic Expressions
INORDER TRAVERSAL - LDR
D
Inorder Traversal
A+B
L R
PREORDER TRAVERSAL - DLR
Preorder Traversal
+AB
POSTORDER TRAVERSAL - LRD
Postorder Traversal
AB+
Threaded
Binary Tree
Threaded Binary Tree
In-order traversal
BAC
Threaded Binary Tree
LBIT LCHILD DATA RCHILD RBIT
Threaded Binary Tree
Binary Tree
Representation of
Trees
Binary Tree Representation of Trees
• Every tree can be represented as a binary tree.
• One form of representation used variable size
nodes.
• An alternative would be to use fixed size nodes each
node having k child fields if k is the maximum
degree of any node
Binary Tree Representation of Trees
Tree
Binary Tree
Binary Tree presentation of the tree
Graphs
Konigsberg bridge problem
Graphs
• A graph, G, consists of two sets V and E.
• V is a finite non-empty set of vertices.
• E is a set of pairs of vertices, these pairs are called
edges.
• V(G) and E(G) will represent the sets of vertices
and edges of graph G.
• We will also write G = (V,E) to represent a graph.
• Two types of graphs
* Undirected graph
* Directed graph/Digraph
Graphs
• In an undirected graph the pair
of vertices representing any edge
is unordered .
• The pairs (v1, v2) and (v2, v1)
represent the same edge.
• In a directed graph each edge is
represented by a directed pair (v1,
v2). v1 is the tail and v2 the head
of the edge.
• Therefore <v2, v1> and <v1, v2>
represent two different edges
Sample Graphs
Complete Graph
An n vertex undirected graph with exactly n(n - 1)/2 edges is
said to be a complete graph
N= 2
No of edges = 2 * 1 /2 = 1 edge
N= 3
No of edges = 3 * 2 /2 = 3 edges
N= 4
No of edges = 4 * 3 /2 = 6 edges
N= 5
No of edges = 5 * 4 /2 = 10 edges
• In the case of a directed graph on n vertices the
maximum number of edges is n(n - 1)
• Ex: n = 3 no. of edges = 3 * 2 = 6 edges
e1
e2
e6 e5 e3
e4
Edges – Adjacent- incident
e1
1. Adjacency matrices
2. Adjacency lists
3. Adjacency multi-lists
Adjacency Matrices
• Graphs can also be represented in the form of
matrices. Let G = (V,E) be a graph with n vertices,
n ≥ 1.
• The adjacency matrix of G is a 2-dimensional n x n
array, say A, with the property that
• A(i,j) = 1 iff the edge (vi,vj) (<vi,vj> for a directed
graph) is in E(G).
• A(i,j) = 0 if there is no such edge in G.
Adjacency Matrices – Un Directed Graph
Adjacency Matrices – Directed Graph
Adjacency Lists
• The n rows of the adjacency matrix are represented
as n linked lists.
• There is one list for each vertex in G.
• The nodes in list i represent the vertices that are
adjacent from vertex i.
• Each node has at least two fields: VERTEX and
LINK.
Adjacency Lists
The breadth first search traversal order of the above graph is-
A, B, C, D, E, F
Practice –DFS?
The depth first search traversal order of the above graph is-
A,B,E,F,C,D
Connected
Components
Connected Components
• two simple applications of graph traversal:
(i) finding the components of a graph, and
(ii) finding a spanning tree of a connected graph.
• A connected component or simply a component of
an undirected graph is a maximal connected
subgraph
Complete graph
spanning trees
Spanning Trees
• When either DFS or BFS are used the edges of T
form a spanning tree.
• The spanning tree resulting from a call to DFS is
known as a depth first spanning tree.
• When BFS is used, the resulting spanning tree is
called a breadth first spanning tree
Spanning Trees
• depth first spanning tree.
Spanning Trees
• breadth first spanning tree
Applications of Spanning Trees
• Electrical network, Traveling Salesman Problem,
network designs (i.e. telephone or cable networks).
Applications of Spanning Trees
Applications of Spanning Trees
Minimum Cost Spanning Trees
• a spanning tree is a minimal subgraph G' of G such
that V(G') = V(G) and G' is connected
• Any connected graph with n vertices must have at
least n - 1 edges and all connected graphs with n - 1
edges are trees.
• The edges will have weights assigned to them.
• finding a spanning tree of G with minimum cost.
• The cost of a spanning tree is the sum of the costs of
the edges in that tree.
Minimum Cost Spanning Trees
-Kruskal's Algorithm
• a minimum cost spanning tree, T, is built edge by
edge.
• Edges are considered for inclusion in T in
nondecreasing order of their costs.
• An edge is included in T if it does not form a cycle
with the edges already in T.
• Since G is connected and has n > 0 vertices, exactly
n - 1 edges will be selected for inclusion in T.
Minimum Cost Spanning Trees
-Kruskal's Algorithm
Minimum Cost Spanning Trees
-Kruskal's Algorithm
Minimum Cost Spanning Trees -Kruskal's Algorithm
Minimum Cost Spanning Trees –
Kruskal's Algorithm
Spanning Forest
• AOV-network Definition:
A directed graph G in which the vertices
represent tasks or activities and the edges represent
precedence relations between tasks is an Activity On
Vertex network or AOV-network.
Activity Networks
v1
v1 v3 v4
v2
Topological Order
Cl, C13, C4, C8, C14, C15, C5, C2, C3, C10, C7, C11, C12, C6, C9
C13, C14, C15, C5, Cl, C4, C8, C2, C3, C10, C7, C6, C9, Cll, C12.
Topological Order
Topological Order
Indegree(v1)=0
Indegree(v2)=1
Indegree(v3)=1
Indegree(v4)=1
Indegree(v5)=3
Indegree(v6)=2
Critical Paths - Activity On Edge
• An activity network closely related to the AOV-
network is the Activity On Edge or AOE network
• The tasks to be performed on a project are
represented by directed edges.
• Vertices in the network represent events.
• Events signal the completion of certain activities
• Activities represented by edges leaving a vertex
cannot be started until the event at that vertex has
occurred.
• An event occurs only when all activities entering it
have been completed.
Critical Paths - Activity On Edge
Critical Paths - Activity On Edge
• what is the least amount of time in which the project may
be completed (assuming there are no cycles in the
network);
• which activities should be speeded up in order to reduce
completion time etc.
• Several techniques
• PERT (Performance Evaluation and Review Technique)
• CPM (Critical Path Method)
• RAMPS (Resource Allocation and Multi-Project
Scheduling)
Critical Path Method
• The activities in an AOE network can be carried out
in parallel
• the minimum time to complete the project is the
length of the longest path from the start vertex to the
finish vertex
• (the length of a path is the sum of the times of
activities on this path).
• A path of longest length is a critical path.
• The path v1, v2, v5, v7, v9 is a critical path. The
length of this critical path is 18.
• A network may have more than one critical path
Critical Path Method
• The earliest time an event vi occur is the length of the
longest path from the start vertex v1 to the vertex vi.
• Denote this time by e(i) for activity ai.
• the latest time, l(i), an activity may start without
increasing the project duration
• All activities for which e(i) = l(i) are called critical
activities. The difference l(i) - e(i) is a measure of the
criticality of an activity.
• It gives the time by which an activity may be delayed
or slowed without increasing the total time needed to
finish the project.
Critical Path Method
• The purpose of critical path analysis is to identify
critical activities so that
• resources may be concentrated on these activities in
an attempt to reduce project finish time.