Upload files to "DS/C/Lab/Week12"

This commit is contained in:
Aadit Agrawal 2024-10-15 11:13:31 +05:30
parent 8e68c6a751
commit 325008fe63
4 changed files with 291 additions and 0 deletions

92
DS/C/Lab/Week12/BFS.c Normal file
View File

@ -0,0 +1,92 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
typedef struct Node {
int vertex;
struct Node* next;
} Node;
typedef struct Graph {
int numVertices;
Node** adjLists;
} Graph;
typedef struct Queue {
int items[MAX_VERTICES];
int front, rear;
} Queue;
Graph* createGraph(int vertices) {
Graph* graph = malloc(sizeof(Graph));
graph->numVertices = vertices;
graph->adjLists = malloc(vertices * sizeof(Node*));
for (int i = 0; i < vertices; i++) graph->adjLists[i] = NULL;
return graph;
}
void addEdge(Graph* graph, int src, int dest) {
Node* newNode = malloc(sizeof(Node));
newNode->vertex = dest; newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = malloc(sizeof(Node));
newNode->vertex = src; newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
Queue* createQueue() {
Queue* queue = malloc(sizeof(Queue));
queue->front = queue->rear = -1;
return queue;
}
int isEmpty(Queue* queue) {
return queue->rear == -1;
}
void enqueue(Queue* queue, int value) {
if (queue->rear == MAX_VERTICES - 1) return;
if (queue->front == -1) queue->front = 0;
queue->items[++queue->rear] = value;
}
int dequeue(Queue* queue) {
if (isEmpty(queue)) return -1;
int item = queue->items[queue->front];
if (queue->front >= queue->rear) queue->front = queue->rear = -1;
else queue->front++;
return item;
}
void bfs(Graph* graph, int startVertex) {
int visited[MAX_VERTICES] = {0};
Queue* queue = createQueue();
visited[startVertex] = 1; enqueue(queue, startVertex);
while (!isEmpty(queue)) {
int currentVertex = dequeue(queue);
printf("%d ", currentVertex);
for (Node* temp = graph->adjLists[currentVertex]; temp; temp = temp->next) {
int adjVertex = temp->vertex;
if (!visited[adjVertex]) {
visited[adjVertex] = 1; enqueue(queue, adjVertex);
}
}
}
printf("\n");
}
int main() {
Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 5);
printf("BFS traversal starting from vertex 0:\n");
bfs(graph, 0);
return 0;
}

59
DS/C/Lab/Week12/DFS.c Normal file
View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
typedef struct Node {
int vertex;
struct Node* next;
} Node;
typedef struct Graph {
int numVertices;
Node** adjLists;
} Graph;
Graph* createGraph(int vertices) {
Graph* graph = malloc(sizeof(Graph));
graph->numVertices = vertices;
graph->adjLists = malloc(vertices * sizeof(Node*));
for (int i = 0; i < vertices; i++) graph->adjLists[i] = NULL;
return graph;
}
void addEdge(Graph* graph, int src, int dest) {
Node* newNode = malloc(sizeof(Node));
newNode->vertex = dest; newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = malloc(sizeof(Node));
newNode->vertex = src; newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
void dfsUtil(Graph* graph, int vertex, int* visited) {
visited[vertex] = 1;
printf("%d ", vertex);
for (Node* temp = graph->adjLists[vertex]; temp; temp = temp->next) {
if (!visited[temp->vertex]) dfsUtil(graph, temp->vertex, visited);
}
}
void dfs(Graph* graph, int startVertex) {
int visited[MAX_VERTICES] = {0};
dfsUtil(graph, startVertex, visited);
printf("\n");
}
int main() {
Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 5);
printf("DFS traversal starting from vertex 0:\n");
dfs(graph, 0);
return 0;
}

View File

@ -0,0 +1,87 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int vertex;
struct Node* next;
} Node;
typedef struct Graph
{
int vertices;
Node** adjList;//array of ll
} Graph;
Node* createNode(int vertex)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->vertex = vertex;
newNode->next = NULL;
return newNode;
}
Graph* createGraph(int vertices) //vertices is the number of vertices
{
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->vertices = vertices;// no of vertices bataye
graph->adjList = (Node**)malloc(vertices * sizeof(Node*));//adjlist ko sahi size diya ie saare vertices
for (int i = 0; i < vertices; i++)
graph->adjList[i] = NULL;//default setup
return graph;
}
void addEdgeUndirected(Graph* graph, int start, int end)//inset begin of nodes
{
Node* newNode = createNode(end);
newNode->next = graph->adjList[start];
graph->adjList[start] = newNode;
newNode = createNode(start);
newNode->next = graph->adjList[end];
graph->adjList[end] = newNode;
}
void addEdgeDirected(Graph* graph, int start, int end)
{
Node* newNode = createNode(end);
newNode->next = graph->adjList[start];
graph->adjList[start] = newNode;
}
void displayGraph(Graph* graph)
{
for (int i = 0; i < graph->vertices; i++)
{
Node* current = graph->adjList[i];
printf("Adjacency list for vertex %d: ", i);
while (current != NULL)
{
printf("%d -> ", current->vertex);
current = current->next;
}
printf("NULL\n");
}
}
int main() {
int vertices, edges, i, start, end, isDirected;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
Graph* graph = createGraph(vertices);
printf("Is the graph directed? (1 for yes, 0 for no): ");
scanf("%d", &isDirected);
printf("Enter the number of edges: ");
scanf("%d", &edges);
printf("Enter the edges (start and end vertex) separated by space:\n");
for (i = 0; i < edges; i++)
{
scanf("%d %d", &start, &end);
if (isDirected)
addEdgeDirected(graph, start, end);
else
addEdgeUndirected(graph, start, end);
}
displayGraph(graph);
return 0;
}

View File

@ -0,0 +1,53 @@
#include <stdio.h>
#define MAX_VERTICES 100
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
void initializeGraph(int v)
{
int i, j;
for (i = 0; i < v; i++)
{
for (j = 0; j < v; j++)
adjMatrix[i][j] = 0;
}
}
void addEdge(int start, int end, int isDir)
{
adjMatrix[start][end] = 1;
if (!isDir)
adjMatrix[end][start] = 1;
}
void display(int v)
{
int i, j;
printf("Adjacency Matrix:\n");
for (i = 0; i < v; i++)
{
for (j = 0; j < v; j++)
printf("%d ", adjMatrix[i][j]);
printf("\n");
}
}
int main()
{
int v, e, i, start, end, isDir;
printf("Enter the number of vertices: ");
scanf("%d", &v);
initializeGraph(v);
printf("Is the graph directed? (1 for yes, 0 for no): ");
scanf("%d", &isDir);
printf("Enter the number of edges: ");
scanf("%d", &e);
printf("Enter the edges (start and end vertex) separated by space:\n");
for (i = 0; i < e; i++)
{
scanf("%d %d", &start, &end);
addEdge(start, end, isDir);
}
display(v);
return 0;
}