Algorithm Unit 2 Notes
Algorithm Unit 2 Notes
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.
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.
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.
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.
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:
2. Incidence Matrix
In Incidence matrix representation, graph can be represented using a matrix of size:
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.
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:
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.
1. Directed Graph
2. Undirected graph
3. Weighted graph
4. Simple graph
5. Multigraph
6. Complete graph
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.
o Cluster Analysis
o Civil network planning
o Computer network routing protocol
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.