0% found this document useful (0 votes)
109 views

Data Structure Unit II

The document provides an overview of data structures and algorithms related to trees and graphs. It discusses tree representations and traversals, including binary trees. It also covers graph representations and basic graph concepts such as paths, connectedness, and subgraphs. Common graph representations like adjacency matrices and lists are described.

Uploaded by

palailalitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Data Structure Unit II

The document provides an overview of data structures and algorithms related to trees and graphs. It discusses tree representations and traversals, including binary trees. It also covers graph representations and basic graph concepts such as paths, connectedness, and subgraphs. Common graph representations like adjacency matrices and lists are described.

Uploaded by

palailalitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 139

DATA STRUCTURES

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

• CEO- Chief Executive Officer


• COO – Chief Operating Officer
• CFO – Chief Financial Officer
Tree - Terminologies
Node
A node stands for the item of
information plus the branches to other
items.
Ex: A,B,C,D,E,F,G etc are nodes.
Degree
The number of subtrees of a
node/children is called its degree.
Ex: Degree of A = 2, B=3,C=2, K=0
Leaf/ terminal nodes
Nodes that have degree zero are
called leaf or terminal nodes.
Ex: D,F,H,I,J,K are leaf nodes.
Tree - Terminologies
Nonterminal
Nodes other than leaf nodes are
referred to as nonterminal
Ex: A,B,C,E,G are nonterminal nodes
Children, Parent
The roots of the subtrees of a node, X,
are the children of X. X is the parent of
its children.
Ex: A is parent of B,C.
G,H are children of C
Siblings
Children of the same parent are said to
be siblings. Ex: I and J
Degree of a Tree
Degree of a tree
The degree of a tree is the maximum
degree of the nodes in the tree.
Ex: Degree of Tree = 3
Ancestors
The ancestors of a node are all the
nodes along the path from the root
to that node.
Ex: Ancestor of I is E,B,A
Path
Path
Path is a sequence of nodes and
edges between two nodes
Level
• The level of a node is
defined by initially
letting the root be at
level one.
• If a node is at level l,
then its children are at
level l + 1.
• The height or depth
of a tree is defined to
be the maximum level
of any node in the
tree.
• Example:
Height/depth =3
Subtrees
Forest
• A forest is a set of n≥0 disjoint trees.
• if we remove the root of a tree we get a forest.
Sequential Representation of Trees
Linked List Representation
Binary Trees
&
Representations
Binary Tree
A binary tree is a finite set of nodes
which is
• either empty or
• consists of a root and two disjoint
binary trees called the left subtree
and the right subtree.
Binary Tree
Skewed Binary Trees &
Complete Binary Tree
Lemma
• The maximum number of
nodes on level i of a binary
tree is 2i-1, i ≥ 1
• Ex: Level 3 can have a
maximum of 23-1=22=4
nodes
• The maximum number of
nodes in a binary tree of
depth k is 2k - 1, k ≥ 1
• Ex: depth=4
• maximum of 24 -1 =16-1
=15 nodes
Full Binary Tree
• A full binary tree of depth k is a binary tree of
depth k having 2k - 1 nodes.
• Ex: depth=4, nodes=15
Complete Binary Tree
• Complete Binary Tree: A Binary Tree is a complete
Binary Tree if all the levels are completely filled
except possibly the last level and the last level has all
keys as left as possible
Complete Binary Tree
• Complete Binary Tree:
Complete Binary 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 the linked representation of any binary tree


replace the null links by pointers, called threads, to
other nodes in the tree.
• If the RCHILD(P) is equal to zero, replace it by a
pointer to the node which would be printed after P
when traversing the tree in in-order.
• If the LCHILD(P) is equal to zero, replace it by a
pointer to the node which immediately precedes
node P in in-order.
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

• If (v1,v2) is an edge in E(G), then we shall say the


vertices v1 and v2 are adjacent and that the edge
(v1,v2) is incident on vertices v1 and v2.
• If <v1,v2> is a directed edge, then vertex v1 will be
said to be adjacent to v2 while v2 is adjacent from
v1.
• The edge <v1,v2> is incident to v1 and v2.
Degree of Vertices
• The degree of a vertex is the number of edges incident to
that vertex.
Degree
• In a directed graph, we define the in-degree of a
vertex v to be the number of edges for which v is the
head.
• The out-degree is defined to be the number of edges
for which v is the tail.
Subgraph
• A subgraph of G is a graph G' such that V(G') V(G)
and E(G') E(G).
• some of the subgraphs of G1
Graphs -Path
• A path from vertex vp to vertex vq in graph G is a
sequence of vertices vp,vi1,vi2, ...,vin,vq such that
(vp,vi1),(vi1,vi2), ...,(vin,vq) are edges in E(G).
• If G' is directed then the path consists of <vp,vi1>,
<vi,vi2>, ..., <vin,vq>, edges in E(G’).

Path from 1 to 6 -- (1,5), (5,2),(2,3),(3,4),(4,6)


Length of the path = 5
Graphs -Path
• The length of a path is the number of edges on it.
• A simple path is a path in which all vertices except
possibly the first and last are distinct.
• A cycle is a simple path in which the first and last
vertices are the same.
• In an undirected graph, G, two vertices v1 and v2
are said to be connected if there is a path in G from
v1 to v2 (since G is undirected, this means there
must also be a path from v2 to v1).
• Vertices 1 & 6 are connected
Connected graph
• An undirected graph is said to
be connected
• if for every pair of distinct
vertices vi, vi in V(G)
• there is a path from vi to vj in
G.
• A directed graph G is said to
be strongly connected if for
every pair of distinct vertices
vi, vj in V(G) there is a
directed path from vi to vj and
also from vj to vi
Graph
Representations
Graph Representations

Three most commonly used Graph Representations

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 VERTEX fields contain the indices of the


vertices adjacent to vertex i.
• Each list has a head node.
• The head nodes are sequential providing easy
random access to the adjacency list for any
particular vertex.
Adjacency Lists – Directed Graph
Adjacency Lists –Directed Graph
Adjacency Multi lists
• In the adjacency list representation of an undirected
graph each edge (vi,vj) is represented by two entries,
one on the list for vi and the other on the list for vj.
• Node structure

• M indicates node is visited or not


Adjacency Multi lists
Graph
Traversals
Graph Traversals
• Given an undirected graph G = (V,E) and a vertex v
in V(G)
• we are interested in visiting all vertices in G that are
reachable from v (i.e., all vertices connected to v)

• Depth First Search


• Breadth First Search.
Depth First
Search
Depth First Search Traversal

vertices of G are visited in the order:


v1, v2, v4, v8, v5, v6, v3, v7
Depth First Search Traversal
• The start vertex v is visited.
• Next an unvisited vertex w adjacent to v is selected
and a depth first search from w initiated.
• When a vertex u is reached such that all its adjacent
vertices have been visited, we back up to the last
vertex visited which has an unvisited vertex w
adjacent to it and initiate a depth first search from
w.
• The search terminates when no unvisited vertex can
be reached from any of the visited one.
Depth First Search Traversal
Breadth First
Search
Breadth First Search
Traversal

v1 and then v2 and v3, v4, v5, v6 and v7, v8


Breadth First Search
• Starting at vertex v and marking it as visited
• breadth first search differs from depth first search
• all unvisited vertices adjacent to v are visited next.
• Then unvisited vertices adjacent to these vertices
are visited and so on.
• Recursive procedure
Breadth First Search
Practice –DFS? & BFS?
Practice –BFS?

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

G4 has two components H1 and H2


Connected Components
• A strongly connected component is a maximal
subgraph that is strongly connected.
• Example: G3 has two strongly connected
components.
Connected Components
• By the definition of a connected component, there is
a path between every pair of vertices in the
component and
• there is no path in G from vertex v to w if v and w
are in two different components.
• determine all the connected components of a graph
• all the vertices visited, together with all edges in G
incident to these vertices form a connected
component of G.
• This is done by repeated calls to either DFS(v) or
BFS(v), with v a vertex not yet visited.
Connected Components
Spanning Trees
Spanning Trees
• Any tree consisting only of edges in G and
including all vertices in G is called a spanning tree.

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

• When there is only one connected component in your


graph, the spanning tree = spanning forest.
• When there are multiple connected components in the
graph then for each component, we have a spanning
tree
SHORTEST PATHS

• The length of a path is now defined to be the sum of


the weights of the edges on that path rather than the
number of edges.
• The starting vertex of the path will be referred to as
the source and the last vertex the destination. The
graphs will be digraphs to allow for one way streets.
• It is assumed that all weights are positive.
Single Source All Destinations
• We are given a directed graph G = (V,E) a
weighting function w(e) for the edges of G
• and a source vertex vo. The problem is to determine
the shortest paths from vo to all the remaining
vertices of G.
• It is assumed that all the weights are positive.
Single Source All Destinations
Dijkstra’s Shortest Path Algorithm
All Pairs Shortest Paths
• The all pairs shortest path problem finds the shortest
paths between all pairs of vertices vi,vj, i < >j.
• One possible solution to this is to apply the
algorithm SHORTEST__ PATH n times, once with
each vertex in V(G) as the source.
• For the all pairs problem, a more simpler algorithm
which will work even when some edges in G have
negative weights so long as G has no cycles with
negative length.
All Pairs Shortest Paths
All Pairs Shortest Paths
Transitive Closure
• If A is the adjacency matrix of G, then the matrix
A+ having the property A+ (i,j) = 1 if there is a path
of length > 0 from i to j and 0 otherwise is called
the transitive closure matrix of G

• The matrix A* with the property A* (i,j) = 1 if there


is a path of length >=0 from i to j and 0 otherwise is
the reflexive transitive closure matrix of G.
Transitive Closure
Activity Networks
Topological Sort
Critical Path Method
Activity Networks
• A project can be subdivided into several subprojects
called activities.

• The successful completion of these activities will


result in the completion of the entire project.

• 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

• Vertex i in an AOV network G is a predecessor of


vertex j iff there is a directed path from vertex i to
vertex j
• If i is a predecessor of j, then j is a successor of i.
• i is an immediate predecessor of j iff <i,j> is an edge
in G.
• If i is an immediate predecessor of j, then j is an
immediate successor of i.
Activity Networks
Activity Networks
Activity Networks
Activity Networks
Activity Networks
Activity Networks
• Definition:
A relation R is irreflexive on a set S if for no
element x in S it is the case that
xRx
• A relation R is transitive on a set S if for no
element x in S it is the case that
x R y and y R z ⇒ x R z

• A precedence relation which is both transitive and


irreflexive is a partial order.
Activity Networks

• Given an AOV network one of our concerns would


be to determine whether or not the precedence
relation defined by its edges is irreflexive.

• This is identical to determining whether or not the


network contains any directed cycles. A directed
graph with no directed cycles is an acyclic graph
Topological Order
• Our algorithm to test an AOV-network for
feasibility will also generate a linear ordering,
vi1, vi2, ...,vin, of the vertices (activities).

• This linear ordering will have the property that if i


is a predecessor of j in the network then i precedes j
in the linear ordering. A linear ordering with this
property is called a topological order.
Topological Order

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.

You might also like