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

Algorithm Unit 2 Notes

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

Algorithm Unit 2 Notes

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

UNIT 2: ALGORITHM

One-way threaded Binary trees:

In one-way threaded binary trees, a thread will appear either in the right or left link field
of a node. If it appears in the right link field of a node then it will point to the next node
that will appear on performing in order traversal. Such trees are called Right threaded
binary trees.

Two-way threaded Binary Trees:


In two-way threaded Binary trees, the right link field of a node containing NULL values is replaced by a thread
that points to nodes inorder successor and left field of a node containing NULL values is replaced by a thread
that points to nodes inorder predecessor.

Types of Binary Tree


There are four types of Binary tree:

o Full/ proper/ strict Binary tree


o Complete Binary tree
o Perfect Binary tree
o Degenerate Binary tree
o Balanced Binary tree

1. Full/ proper/ strict Binary tree

o The full binary tree is also known as a strict binary tree. The tree can only be
considered as the full binary tree if each node must contain either 0 or 2 children.
The full binary tree can also be defined as the tree in which each node must
contain 2 children except the leaf nodes.
o Let's look at the simple example of the Full Binary tree.

Complete Binary Tree

The complete binary tree is a tree in which all the nodes are completely filled except the
last level. In the last level, all the nodes must be as left as possible. In a complete binary
tree, the nodes should be added from the left.Let's create a complete binary tree.

Perfect Binary Tree

A tree is a perfect binary tree if all the internal nodes have 2 children, and all the leaf
nodes are at the same level.

Degenerate Binary Tree: The degenerate binary tree is a tree in which all
the internal nodes have only one children. Let's understand the Degenerate binary
tree through examples.
Balanced Binary Tree

The balanced binary tree is a tree in which both the left and right trees differ by atmost
1. For example, AVL and Red-Black trees are balanced binary tree.

Let's understand the balanced binary tree through examples.

Graph Representations
To represent a graph, we just need the set of vertices, and for each vertex the neighbors
of the vertex (vertices which is directly connected to it by an edge). If it is a weighted
graph, then the weight will be associated with each edge.

There are different ways to optimally represent a graph, depending on the density of its
edges, type of operations to be performed and ease of use.

1. Adjacency Matrix
o Adjacency matrix is a sequential representation.
o It is used to represent which nodes are adjacent to each other. i.e. is there any
edge connecting nodes to a graph.
o If there is any weighted graph then instead of 1s and 0s, we can store the weight
of the edge.

Example
Consider the following undirected graph representation:

Undirected graph representation

Directed graph represenation

See the directed graph representation:


In the above examples, 1 represents an edge from row vertex to column vertex, and 0
represents no edge from row vertex to column vertex.

Undirected weighted graph represenation

2. Incidence Matrix
In Incidence matrix representation, graph can be represented using a matrix of size:

Total number of vertices by total number of edges.

It means if a graph has 4 vertices and 6 edges, then it can be represented using a matrix
of 4X6 class. In this matrix, columns represent edges and rows represent vertices.

This matrix is filled with either 0 or 1 or -1. Where,

o 0 is used to represent row edge which is not connected to column vertex.


o 1 is used to represent row edge which is connected as outgoing edge to column
vertex.
o -1 is used to represent row edge which is connected as incoming edge to column
vertex.

Example
Consider the following directed graph representation.
3. Adjacency List
o Adjacency list is a linked representation.
o In this representation, for each vertex in the graph, we maintain the list of its neighbors.
It means, every vertex of the graph contains list of its adjacent vertices.
o We have an array of vertices which is indexed by the vertex number and for each vertex
v, the corresponding array element points to a singly linked list of neighbors of v.
o The adjacency list allows testing whether two vertices are adjacent to each other
but it is slower to support this operation.

Example
Let's see the following directed graph representation implemented using linked list:

What are connected graphs in data structure?


A graph is a non-linear data structure with a finite number of vertices and edges, and these
edges are used to connect the vertices. Multiple runs are required to traverse through all the
elements completely. Traversing in a single run is impossible to traverse the whole data
structure.

Properties of connected graphs

o It is used to store the data elements combined whenever they are not present in
the contiguous memory locations.
o It is an efficient way of organizing and properly holding the data.
o Data is stored randomly in memory.
o It is comparatively difficult to implement.
o Multiple levels are involved.
o Memory utilization is effective.

Types of Connected Graph:

1. Directed Graph
2. Undirected graph
3. Weighted graph
4. Simple graph
5. Multigraph
6. Complete graph

Let us discuss some of its types are:

1. Directed Graph: The graph consists of a directed set of edges in which every
edge is associated with a particular direction. These directing edges direct the
direction of the path from one vertex to another.
2. Undirected graph: The graph consists of an undirected set of edges, in which
every edge is connected with the vertexes but does not specify a particular
direction.
3. Weighted graph: The weighted graph is the graph that consists of the edges
having some weights over them; it means edges consist of some value for going
from one vertex to the other.
4. Simple graph: Simple graph is defined as a graph that consists of only one edge
between the two vertexes. Simple graphs do not consist of any parallel edges and
self-loops.
5. Multigraph: Multigraph consists of parallel edges and self-loops. It is more
complex as compared to simple graphs.
6. Complete graph: Complete graph is how each vertex is connected with every
other vertex. In a complete graph, each vertex's degree must be n - 1, where n is
denoted as the number of vertices.

Spanning tree
A spanning tree can be defined as the subgraph of an undirected connected graph. It includes
all the vertices along with the least possible number of edges. If any vertex is missed, it is not a
spanning tree. A spanning tree is a subset of the graph that does not have cycles, and it also
cannot be disconnected.

Applications of the spanning tree


Basically, a spanning tree is used to find a minimum path to connect all nodes of the
graph. Some of the common applications of the spanning tree are listed as follows -

o Cluster Analysis
o Civil network planning
o Computer network routing protocol

Applications of minimum spanning tree


The applications of the minimum spanning tree are given as follows -

o Minimum spanning tree can be used to design water-supply networks,


telecommunication networks, and electrical grids.
o It can be used to find paths in the map.

Example of Spanning tree


Suppose the graph be -
Algorithms for Minimum spanning tree
A minimum spanning tree can be found from a weighted graph by using the algorithms
given below -

o Prim's Algorithm
o Kruskal's Algorithm

Prim's algorithm - It is a greedy algorithm that starts with an empty spanning tree. It is used to
find the minimum spanning tree from the graph. This algorithm finds the subset of edges that
includes every vertex of the graph such that the sum of the weights of the edges can be
minimized.

Kruskal's algorithm - This algorithm is also used to find the minimum spanning tree for a
connected weighted graph. Kruskal's algorithm also follows greedy approach, which finds an
optimum solution at every stage instead of focusing on a global optimum.
What are the Shortest Path Algorithms?
The shortest path algorithms are the ones that focuses on calculating the
minimum travelling cost from source node to destination node of a graph
in optimal time and space complexities.

Types of Shortest Path Algorithms:


1. Depth-First Search (DFS)
2. Breadth-First Search (BFS)
1. Shortest Path Algorithm using Depth-First Search(DFS):
DFS algorithm recursively explores the adjacent nodes untill it reaches to the
depth where no more valid recursive calls are possible.
For DFS to be used as a shortest path algorithm, the graph needs to be
acyclic i.e. a TREE, the reason it won’t work for cyclic graphs is because due
to cycles, the destination node can have multiple paths from the source node
and dfs will not be able to choose the best path.
If there does not exist a path between source node and destination node
then we can store -1 as the shortest path between those nodes.
Algorithm:
 Distance of source node to source node is initialized to 0.
 Start the DFS from the source node.
 As we go to a neighbouring nodes we set it’s distance from source node =
edge weight + distance of parent node.
 DFS ends when all the leaf nodes are visited.
Complexity Analysis:
 Time Complexity: O(N) where N is the number of nodes in the tree.
 Auxiliary Space: O(N) call stacks in case of skewed tree.
2. Breadth-First Search (BFS) for Shortest Path Algorithm:
BFS is a great shortest path algorithm for all graphs, the path found by
breadth first search to any node is the shortest path to that node, i.e the path
that contains the smallest number of edges in unweighted graphs.
Reason: This works due to the fact that unlike DFS, BFS visits all the
neighbouring nodes before exploring a single node to its neighbour. As a
result, all nodes with distance ‘d‘ from the source are visited after all the
nodes with distance smaller than d. In simple terms all the nodes with
distance 1 from the node will be visited first, then distance 2 nodes, then 3
and so on.
Algorithm:
 Use a queue to store a pair of values {node, distance}
 Distance of source node to source node is initialized to 0 i.e. push {src,
0} to the queue
 While queue is not empty do the following:
o Get the {node, distance} value of top of the queue
o Pop the top element of the queue
o Store the distance for the node as the shortest distance for that
node from the source
o For all non visited neighbours of the current node,
push {neighbour, distance+1} into the queue and mark it visited
Complexity Analysis:
 Time Complexity: O(N) where N is the number of nodes in the graph
 Auxiliary Space: O(N) for storing distance for each node

You might also like