Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Data Structures And Algorithm(sem-II)-1

The document provides an overview of data structures, focusing on trees and graphs, including their definitions, types, terminologies, and operations. It details various tree types such as binary trees, binary search trees, and heaps, as well as graph concepts like directed and undirected graphs, graph traversal methods, and algorithms for minimum spanning trees. Additionally, it covers Dijkstra's algorithm for finding the shortest path in graphs and includes practice questions for better understanding.

Uploaded by

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

Data Structures And Algorithm(sem-II)-1

The document provides an overview of data structures, focusing on trees and graphs, including their definitions, types, terminologies, and operations. It details various tree types such as binary trees, binary search trees, and heaps, as well as graph concepts like directed and undirected graphs, graph traversal methods, and algorithms for minimum spanning trees. Additionally, it covers Dijkstra's algorithm for finding the shortest path in graphs and includes practice questions for better understanding.

Uploaded by

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

Data Structures And

Algorithm
Sem-II
❖ TREE (non-linear Data Structure)
A tree is a finite set of nodes with one specially designated node called as root and

the remaining nodes are partition into sets called as subtrees of the root.

Applications :- 1) Game Trees


2) Heap sort
3) Representing hierarchical information
❖ Terminologies in Tree

• Null Tree- no nodes in tree.

• Parent node – Is a node having other nodes connected to it.

• Leaf node – does not have child node.

• Siblings – children of same parent.

• Degree of a node – number of sub-tree connected

to that node.

• Level of a node – indicate the position of node in

hierarchy (root=level 0)

• Height of tree – total number of levels in tree

• Ancestors – All the nodes from that node to the root


❖ Types of Tree

1) Binary Tree – It a tree where every node can have at the


most two branches(children)

❑ Types of Binary Tree


1) Full Binary Tree
2) Strictly Binary Tree
3) Complete Binary Tree
4) Skewed Binary Tree

1.1) Full Binary Tree – Each node have 0 or 2 child.

1.2) Strictly Binary Tree – Where all non-leaf nodes(parent) have two branches.
1.3) Complete Binary Tree - Completely filled expect last level(d-1) and last level filled
from left to right

1.4) Skewed Binary Tree (Degenerate Tree) – All nodes have only one child
Left skewed – all nodes only in left subtrees
Right skewed – all nodes only in right subtrees

2) Binary Search Tree – It is a binary tree in which the node are arranged
according to their values. The left node has a value<its parent and
right node has a value≥parent node.
3) Expression Tree – represents expression containing operators
and operands.

4) Heap Tree – is complete binary tree such that the key in the parent is greater than or
equal to the keys in children . Largest value in root node it is called Max heap. smallest
value in root node it is called Min heap.
❖ Representation of Binary Tree

1. Static representation using array.

2. Dynamic representation using linked organization.

typedef struct node


A
{
C B int info;
struct node *left,*right;
D E H
} node;
❖ Operations on Tree

1) Create – Create a tree with a nodes.

2) Traverse – visits each and every node of tree.

Preorder(Data-Left-Right)

Inorder(Left-Data-Right)

Postorder(Left-Right-Data)

3) Search – search for specific element in tree.

4) Insert – insert a new element in tree.

5) Delete – remove the node from tree.

6) Counting leaf nodes

7) Mirror

8) Counting total nodes


❖ Questions for practices
1) Find leaf nodes.

2) Find non-leaf nodes.

3) Ancestors of 50 and 90.

4) How many levels in tree.

5) What is height of tree.

6) Parent node of 10.

7) Pairs of siblings.

8) How many nodes are there in level 2.

9) Left subtree of 12 or 79.

10) 50 and 20 are siblings or not.


❖ Level order traversal

0
1
2
3
❖ Graph :- A graph is non-linear data structure consisting vertices(node) and
edges(connections between nodes)

OR
Is a collection of two sets V and E (G=(V,E)).V is finite non-empty sets of vertices
and E is finite non-empty sets of edges connecting pairs of vertices.

❖ Applications :- 1) representing maps and


computer network
2) representing projects as
AOV and AOE

❖ Types of graph
1) Undirected Graph :- the edge do not have a direction.
This means the relationship between two nodes is bidirectional.

2) Directed Graph :- The edge have direction and the relationship


is one-way.
❖ Terminologies in Tree

1) Vertex(node) :- a unit of graph.

2) Edge :- Connections between vertices.

3) Indegree of vertex :- number of incoming edges to the vertex.

4) Outdegree of vertex :- number of out going edges .

5) Degree :- total number of edges that vertex is connected.

(Degree=indegree + outdegree)

6) Path :- A sequence of edges that allows to traversal from one

vertex to another

7) Cycle :- A path that starts and ends at the same vertex without

repeating any edges or vertices.

8) Weighted Graph :- A number is associated with each edge of graph.


8) Subgraph :- The vertices and edges of a graph that are subsets of another graph.

9) Spanning tree :- Is a subsets of graph with all vertices and n(number of vertices)-1 edges.

Eg :- In this graph there are vertices=3


then in spanning there must be 3-1
edges .
❖ Graph Traversals

1) DFS (Depth First Search)


• It explore only one vertex at a time
• uses Stack for traversal.
• It uses Backtracking strategy.
• DFS ={a,b,e,h,f,i,c,d,g}

2) BFS (Breadth First Search)


• It explore all vertices that are connect to that node.
• uses Queue for traversal.
• It doesn’t use Backtracking strategy.
• BFS ={a,b,c,d,e,f,g,h,i}
❖ Graph Representation

1) Adjacency Matrix ->

0 0 0
1 0 1
1 0 0

2) Adjacency List ->


Example :-
❖ Practice Question 1)

2) Adjacency List :-
❖ Topological Sort

• It is linear ordering of graph vertices such that for every directed edge UV

from vertex u to vertex V, U comes before V in the ordering.

• Applicable on DAG(Directed Acyclic Graph).

❖ Steps to find out Topological Sort

1) Calculate Indegree of all vertices.

2) Delete vertex which has indegree=0.

3) After deleting the vertex of indegree=0 ,Again calculate Indegree of all vertex .

4) Repeat the process until all vertex visited.


Step 3) Calculate indegree after deleting vertex 0
❖ Example :-
0 1 2 3
0 0 1 2

Step 4) Delete 1 vertex because indegree=0


Graph will be
Step 1) Calculate indegree
0 1 2 3
0 1 2 2 Visited :- 0 1

Step 5) Calculate indegree after deleting vertex 1


Step 2) Delete 0 vertex because indegree=0
Graph will be 0 1 2 3
0 0 0 1

Step 6) Delete 2 vertex because indegree=0


Graph will be

Visited :- 0 3
Visited :- 0 1 2
Step 7) Calculate indegree after deleting vertex 2
0 1 2 3
0 0 0 0

Step 8) Delete 3 vertex because indegree=0


no graph(Empty)

Visited :- 0 1 2 3
❖ Practice Question

Answer
Topological Sort :- 7 6 5 4 3 2 1 0
OR
75643210
❖ There are two algorithm for finding the minimum spanning tree of graph :-

1. Prim’s Algorithm

2. Kruskal’s Algorithm

❖ Prim’s Algorithm

To start algorithm, all loops and parallel edges are removed from the graph. The algorithm construct

the spanning tree by adding one edge at a time.

The edge is selected by considering three factors :-1) select the edge which has lowest cost.

2) edge should not form a cycle

3) lowest cost edge is search on start vertex as well

as end vertex.
❖ Practice Question
Answer
❖ Kruskal’s Algorithm

To start algorithm, all loops and parallel edges are removed from the graph. The algorithm

construct the spanning tree by adding edge.

The edge is selected by considering two factors :-1) select the edge which has lowest cost from

graph.

2) edge should not form a cycle in graph.


❖ Practice Questions
❖ Dijkstra’s Algorithm :-

• Dijkstra algorithm is “single source to all destinations” which gives the shortest path

from a given vertex(source) to all other vertices in a graph. It uses the “Greedy”

strategy.

• A greedy is a way of solving a problem step by step by always picking the best option

available at that moment.

• It uses a concept of edge relaxation.

If( dist[v] + cost[v][w](m[v][w]) < dist[w] )

dist[w]= dist[v] + cost[v][w](m[v][w]


• Distance from source to source is 0 and remaining will be

infinity( )
❖ Practice question
❖ Practice Question

3
1

2
0
❖ Extra Practice Questions

➢ Prim’s Algorithm
➢ Kruskal’s Algorithm
➢ Topological Sort
➢ Dijkstra’s Algorithm
➢ Create Adjacency Matrix and Adjacency List

You might also like