DS Module4
DS Module4
DS Module4
• Step 2: The next element is 33. As we know that insertion in the binary tree
always starts from the left side so 44 will be added at the left of 33 as
shown below:
• Step 3: The next element is 77 and it will be added to the right of the 44 as
shown below:
above tree that it does not satisfy the max heap property, i.e., parent node
44 is less than the child 77. So, we will swap these two values as shown
below:
Step 4: The next element is 11. The node 11 is added to the left of 33 as shown below:
• Step 5: The next element is 55. To make it a complete binary tree, we
will add the node 55 to the right of 33 as shown below:
• above figure that it does not satisfy the property of the max heap
because 33<55, so we will swap these two values as shown below:
• Step 6: The next element is 88. The left subtree is completed so we
will add 88 to the left of 44 as shown below:
Graph
• A Graph is a non-linear data structure consisting of nodes and edges.
The nodes are sometimes also referred to as vertices and the edges
are lines or arcs that connect any two nodes in the graph
• A Graph consists of a finite set of vertices(or nodes) and set of Edges
which connect a pair of nodes
• Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E = {01,
12, 23, 34, 04, 14, 13}.
• Graphs are used to solve many real-life problems. Graphs are used to
represent networks. The networks may include paths in a city or
telephone network or circuit network.
• Graphs are also used in social networks like linkedIn, Facebook. For
example, in Facebook, each person is represented with a vertex(or
node). Each node is a structure and contains information like person
id, name, gender, locale etc.
• A graph is a data structure that consists of the following two
components:
1. A finite set of vertices also called as nodes.
2. A finite set of ordered pair of the form (u, v) called as edge.
• The pair is ordered because (u, v) is not the same as (v, u) in case of a
directed graph(di-graph).
• The pair of the form (u, v) indicates that there is an edge from vertex
u to vertex v.
• The edges may contain weight/value/cost.
• The following two are the most commonly used representations of a
graph.
1. Adjacency Matrix
2. Adjacency List
• Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of
vertices in a graph.
• Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an
edge from vertex i to vertex j.
• Adjacency matrix for undirected graph is always symmetric.
• Adjacency Matrix is also used to represent weighted graphs.
• If adj[i][j] = w, then there is an edge from vertex i to vertex j with
weight w.
• Removing an edge takes O(1) time.
• Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are
efficient and can be done O(1).
• Adjacency List:
An array of lists is used.
• The size of the array is equal to the number of vertices. Let the array
be an array[].
• An entry array[i] represents the list of vertices adjacent to the ith
vertex.
• This representation can also be used to represent a weighted graph.
The weights of edges can be represented as lists of pairs.
•
• Following is the adjacency list representation of the above graph.
Graph traversal
• Graph traversal is a search technique to find a vertex in a graph.
• In the search process, graph traversal is also used to determine the
order in which it visits vertices.
• Without producing loops, a graph traversal finds the edges to be
employed in the search process.
• That is, utilizing graph traversal, you can visit all the graph's vertices
without going through a looping path.
• There are two methods for traversing graphs, which are as follows:
• Breadth-First Search or BFS Algorithm
• Depth- First Search or DFS Algorithm
• Breadth-First Search Algorithm
• BFS is the most widely utilized method.
• BFS is a graph traversal approach in which you start at a source node
and layer by layer through the graph, analyzing the nodes directly
related to the source node.
• Then, in BFS traversal, you must move on to the next-level neighbor
nodes.
• According to the BFS, you must traverse the graph in a breadthwise
direction:
• To begin, move horizontally and visit all the current layer's nodes.
• Continue to the next layer.
• Breadth-First Search uses a queue data structure to store the node
and mark it as "visited" until it marks all the neighboring vertices
directly related to it.
• The queue operates on the First In First Out (FIFO) principle, so the
node's neighbors will be viewed in the order in which it inserts them
in the node, starting with the node that was inserted first.
• Applications of BFS algorithm
• The applications of breadth-first-algorithm are given as follows -
• BFS can be used to find the neighboring locations from a given source
location.
• In a peer-to-peer network, BFS algorithm can be used as a traversal method
to find all the neighboring nodes. Most torrent clients, such as BitTorrent,
uTorrent, etc. employ this process to find "seeds" and "peers" in the
network.
• BFS can be used in web crawlers to create web page indexes. It is one of the
main algorithms that can be used to index web pages. It starts traversing
from the source page and follows the links associated with the page. Here,
every web page is considered as a node in the graph.
• BFS is used to determine the shortest path and minimum spanning tree.
• BFS is also used in Cheney's technique to duplicate the garbage collection.
• It can be used in ford-Fulkerson method to compute the maximum flow in a
flow network.
• Time complexity of BFS depends upon the data structure used to
represent the graph. The time complexity of BFS algorithm is O(V+E),
since in the worst case, BFS algorithm explores every node and edge.
In a graph, the number of vertices is O(V), whereas the number of
edges is O(E).
• The space complexity of BFS can be expressed as O(V), where V is the
number of vertices.
• Bredth_First_Serach( G, A ) // G ie the graph and A is the source node
• Let q be the queue
• q.enqueue( A ) // Inserting source node A to the queue
• Mark A node as visited.
• While ( q is not empty )
• B = q.dequeue( ) // Removing that vertex from the queue, which will
be visited by its neighbour
• Processing all the neighbors of B
• For all neighbors of C of B
• If C is not visited, q. enqueue( C ) //Stores C in q to visit its
neighbour
• Mark C a visited
• In a tree-like structure, graph traversal requires the algorithm to visit,
check, and update every single un-visited node.