0% found this document useful (0 votes)
8 views9 pages

Exp 3 To 5 Executed

The document contains C code implementations for Depth-First Traversal (DFT) and Breadth-First Traversal (BFT) using an adjacency list representation of a graph, as well as sorting algorithms including Quicksort and Mergesort. It includes functions for creating graphs, adding edges, and performing traversals, along with user input for graph parameters and sorting elements. The code demonstrates fundamental data structures and algorithms in C programming.

Uploaded by

nagaraju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Exp 3 To 5 Executed

The document contains C code implementations for Depth-First Traversal (DFT) and Breadth-First Traversal (BFT) using an adjacency list representation of a graph, as well as sorting algorithms including Quicksort and Mergesort. It includes functions for creating graphs, adding edges, and performing traversals, along with user input for graph parameters and sorting elements. The code demonstrates fundamental data structures and algorithms in C programming.

Uploaded by

nagaraju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// 3 b) Implement DFT and BFT using Adjacency List

#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
struct Graph {
intnumVertices;
struct Node** adjLists;
int* visited;
};
struct Queue {
int items[100];
int front;
int rear;
};
struct Node* createNode(int vertex) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->vertex = vertex;
newNode->next = NULL;
returnnewNode;
}
struct Graph* createGraph(int vertices) {
int i;
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;

graph->adjLists = malloc(vertices * sizeof(struct Node*));


graph->visited = malloc(vertices * sizeof(int));

for (i = 0; i < vertices; i++) {


graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}

return graph;
}

voidaddEdge(struct Graph* graph, intsrc, intdest) {


// Add edge from src to dest
struct Node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;

// Add edge from dest to src (if the graph is undirected)


newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}

struct Queue* createQueue() {


struct Queue* q = malloc(sizeof(struct Queue));
q->front = -1;
q->rear = -1;
return q;
}

intisEmpty(struct Queue* q) {
if (q->rear == -1)
return 1;
else
return 0;
}

voidenqueue(struct Queue* q, int value) {


if (q->rear == 100 - 1)
printf("\nQueue is full!!");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}

intdequeue(struct Queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
q->front = q->rear = -1;
}
}
return item;
}

void BFT(struct Graph* graph, intstartVertex) {


struct Queue* q = createQueue();
struct Node* temp;

graph->visited[startVertex] = 1;
enqueue(q, startVertex);

printf("Breadth-First Traversal starting from vertex %d: ", startVertex);

while (!isEmpty(q)) {
intcurrentVertex = dequeue(q);
printf("%d ", currentVertex);

temp = graph->adjLists[currentVertex];

while (temp) {
intadjVertex = temp->vertex;

if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
}
printf("\n");
}
voidDFTUtil(struct Graph* graph, int vertex) {
struct Node* temp;
graph->visited[vertex] = 1;
printf("%d ", vertex);
temp = graph->adjLists[vertex];
while (temp) {
intconnectedVertex = temp->vertex;

if (graph->visited[connectedVertex] == 0) {
DFTUtil(graph, connectedVertex);
}
temp = temp->next;
}
}
void DFT(struct Graph* graph, intstartVertex) {
printf("Depth-First Traversal starting from vertex %d: ", startVertex);
DFTUtil(graph, startVertex);
printf("\n");
}

voidresetVisited(struct Graph* graph) {


int i;
for (i = 0; i < graph->numVertices; i++) {
graph->visited[i] = 0;
}
}

int main() {
int vertices, edges, startVertex, choice,i;
struct Graph* graph;
charch;

printf("Enter the number of vertices in the graph: ");


scanf("%d", &vertices);

graph = createGraph(vertices);

printf("Enter the number of edges: ");


scanf("%d", &edges);

printf("Enter the edges (source destination):\n");


for (i = 0; i < edges; i++) {
intsrc, dest;
scanf("%d %d", &src, &dest);
addEdge(graph, src, dest);
}

printf("Enter the starting vertex: ");


scanf("%d", &startVertex);
do
{
printf("Choose the traversal method:\n");
printf("1. Breadth-First Traversal (BFT)\n");
printf("2. Depth-First Traversal (DFT)\n");
scanf("%d", &choice);

switch (choice) {
case 1:
BFT(graph, startVertex);
break;
case 2:
resetVisited(graph); // Reset visited array for a new traversal
DFT(graph, startVertex);
break;
default:
printf("Invalid choice!\n");
break;
}
printf("Do you want to continue:y/n");
ch=getch();
}while(ch=='y');
return 0;
}

[Link]

#include<stdio.h>
void quicksort(int *arr, int low, int high)
{
int pivot, i, j, temp;
if(low < high) {
pivot = low; // select a pivot element
i = low;
j = high;
while(i < j) {
// increment i till you get a number greater than the pivot element
while(arr[i] <= arr[pivot] && i <= high)
i++;
// decrement j till you get a number less than the pivot element
while(arr[j] >arr[pivot] && j >= low)
j--;
// if i < j swap the elements in locations i and j
if(i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

// when i >= j it means the j-th position is the correct position


// of the pivot element, hence swap the pivot element with the
// element in the j-th position
temp = arr[j];
arr[j] = arr[pivot];
arr[pivot] = temp;
// Repeat quicksort for the two sub-arrays, one to the left of j
// and one to the right of j
quicksort(arr, low, j-1);
quicksort(arr, j+1, high);
}
}

int main()
{
intarr[20], n, i;
printf("Enter the size of the array\n");
scanf("%d", &n);

printf("Enter the elements to be sorted\n");


for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
quicksort(arr, 0, n-1);

printf("Sorted array\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);

return 0;
}

[Link]

// C program for Merge Sort


#include <stdio.h>
#include <stdlib.h>

// Merges two subarrays of arr[].


// First subarray is arr[l..m]
// Second subarray is arr[m+1..h]
void merge(intarr[], int l, int m, int h)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = h - m;

// Create temp arrays


int L[20], R[20];

// Copy data to temp arrays


// L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

// Merge the temp arrays back


// into arr[l..h]
// Initial index of first subarray
i = 0;

// Initial index of second subarray


j = 0;
// Initial index of merged subarray
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}

// Copy the remaining elements


// of L[], if there are any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

// Copy the remaining elements of


// R[], if there are any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

// l is for left index and r is


// right index of the sub-array
// of arr to be sorted
voidmergeSort(intarr[], int l, int h)
{
if (l < h) {
// Same as (l+h)/2, but avoids
// overflow for large l and r
int m = l + (h - l) / 2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m + 1, h);

merge(arr, l, m, h);
}
}

// Function to print an array


voidprintArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}

int main()
{
intarr[20],n,i;
clrscr();
printf("Enter the size of the array: \n");
scanf("%d",&n);
printf("Enter Elements to sort: \n");
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

mergeSort(arr, 0, n-1);

printf("\nSorted array is \n");


printArray(arr, n);
return 0;
}

You might also like