Unit-2
Unit-2
int size = 0;
if (largest != i) {
swap(&array[i], &array[largest]);
heapify(array, size, largest);
}
}
// Heapify from the current index to adjust the rest of the heap
if (i < size) {
heapify(array, size, i);
}
}
int main() {
int array[10];
insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2);
printf("Max-Heap array: ");
printArray(array, size);
deleteRoot(array, 4);
printf("After deleting an element: ");
printArray(array, size);
return 0;
}
There are multiple ways to store a graph: The following are the most common
representations.
Adjacency Matrix
Adjacency List
Adjacency Matrix Representation of Graph Data Structure:
In this method, the graph is stored in the form of the 2D matrix where rows and
columns denote vertices. Each entry in the matrix represents the weight of the edge between
those vertices.
// Undirected graph
newNode = createNode(u);
newNode->next = adj[v];
adj[v] = newNode;
}
// Function to perform Breadth First Search on a graph
// represented using adjacency list
void bfs(struct Node* adj[], int vertices,
int source, int visited[]) {
// Create a queue for BFS
int queue[MAX_VERTICES];
int front = 0, rear = 0;
int main() {
int V = 5;
int E = 5;
// Define the edges of the graph
int edges[][2] = {{1, 2}, {1, 0}, {2, 0}, {2, 3}, {2, 4}};
int source = 1;
printf("DFS from source: %d\n", source);
DFS(adj, V, source);
return 0;
}
Connected component:
If G is connected undirected graph, then we can visit all the vertices of the graph in the first
call to BFS. The sub graph which we obtain after traversing the graph using BFS represents
the connected component of the graph.
Spanning tree of a graph: Consider the set of all edges (u, w) where all vertices w are
adjacent to u and are not visited. According to BFS algorithm it is established that this set of
edges give the spanning tree of G, if G is connected. We obtain depth first search spanning
tree similarly.
These are the BFS and DFS spanning trees of the graph G
In many fields, the quantitative discipline is crucial. Graphs are regarded as an excellent
modeling instrument that can be used to simulate various phases of relationships between all
physical circumstances. Graphs are a useful tool for illustrating a variety of real-world issues.
Some significant graph uses are listed below:
Social Networks: Graphs are unique network configurations with just one kind of edge
separating each vertex.
Web Graphs: There are many allusions to URLs on the internet. In other terms, the internet
is a great source of network data.
Biological Networks: Biological networks or space are two important forms of graphs in the
actual world. Brain networks, protein signalling networks, and nutrition networks are a few
examples.
Neural Networks: Large diagrams that artificially link neurons with synapses create neural
networks. There are numerous varieties of neural networks, and the primary distinction
among them is how graphs are formed.
Map Networks: All devices come pre-loaded with applications like Uber, Apple Maps,
Google Maps, and Maze. Models for navigation issues resemble those for graph issues.
Consider issues with moving merchants, issues with shortcuts, Hammington paths, etc.
Blockchains: Each block‟s vertices can contain numerous deals, and the edges link the
blocks that follow. The present benchmark for historical transactions is the biggest branch
from the first block.
Divide and Conquer algorithm consists of a dispute using the following three steps.
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted Array\n");
printArray(arr, n);
return 0;
}
Merge sort :
Merge sort is a sorting algorithm that follows the divide-and-conquer approach. It
works by recursively dividing the input array into smaller sub arrays and sorting those sub
arrays then merging them back together to obtain the sorted array.
In simple terms, we can say that the process of merge sort is to divide the array into two
halves, sort each half, and then merge the sorted halves back together. This process is
repeated until the entire array is sorted.
How does Merge Sort work?
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows
the divide-and-conquer approach to sort a given array of elements.
Here‟s a step-by-step explanation of how merge sort works:
1. Divide: Divide the list or array recursively into two halves until it can no more be
divided.
2. Conquer: Each subarray is sorted individually using the merge sort algorithm.
3. Merge: The sorted subarrays are merged back together in sorted order. The process
continues until all elements from both subarrays have been merged.
#include <stdio.h>
#include <stdlib.h>
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
merge(arr, l, m, r);
}
}
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
Given two square matrices A and B of size n x n each, find their multiplication matrix.
Now the problem remains, how to find the convex hull for the left and right half.
Now recursion comes into the picture, we divide the set of points until the number of
points in the set is very small, say 5, and we can find the convex hull for these points by
the brute algorithm.
The merging of these halves would result in the convex hull for the complete set of
points. Note: We have used the brute algorithm to find the convex hull for a small
number of points and it has a time complexity of O(N^3).
But some people suggest the following; the convex hull for 3 or fewer points is the
complete set of points. This is correct but the problem comes when we try to merge a
left convex hull of 2 points and right convex hull of 3 points, then the program gets
trapped in an infinite loop in some special cases.
So, to get rid of this problem I directly found the convex hull for 5 or fewer points
by O(N^3) algorithm, which is somewhat greater but does not affect the overall
complexity of the algorithm.