0% found this document useful (0 votes)
43 views2 pages

#Include #Define Void Int Int For For

This document contains C code to implement graph operations like initializing a graph, inserting edges, displaying the adjacency matrix, and performing depth-first search (DFS) and breadth-first search (BFS) on graphs. The main function allows the user to choose between displaying the graph, performing DFS, or performing BFS by entering a starting node. Functions are defined to initialize a graph to all zeros, insert edges between two nodes, display the adjacency matrix, and implement the DFS and BFS algorithms.

Uploaded by

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

#Include #Define Void Int Int For For

This document contains C code to implement graph operations like initializing a graph, inserting edges, displaying the adjacency matrix, and performing depth-first search (DFS) and breadth-first search (BFS) on graphs. The main function allows the user to choose between displaying the graph, performing DFS, or performing BFS by entering a starting node. Functions are defined to initialize a graph to all zeros, insert edges between two nodes, display the adjacency matrix, and implement the DFS and BFS algorithms.

Uploaded by

Lo Jo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Λύσεις

Άσκηση 5-1

#include <stdio.h>
#define SIZE 8

void initGraph(int G[SIZE][SIZE]) {


int i, j;
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++)
G[i][j] = 0;
}

void insertEdge(int v, int u, int G[SIZE][SIZE]) {


G[v][u] = 1;
G[u][v] = 1;
}

void displayGraph(int G[SIZE][SIZE]) {


int i, j;
printf("{\n");
for (i = 0; i < SIZE; i++) {
printf(" ");
for (j = 0; j < SIZE; j++) {
printf("%3d", G[i][j]);
}
printf("\n");
}
printf("}\n");
}

void dfs(int G[SIZE][SIZE], int visited[SIZE], int v) {


int u;
visited[v] = 1;
printf(" ->%3d ", v);
for (u = 0; u < SIZE; u++) {
if (!visited[u] && G[v][u] == 1)
dfs(G, visited, u);
}
}

void bfs(int G[SIZE][SIZE], int visited[SIZE], int v) {


int u, front, rear;
int queue[SIZE];
front = rear = 0;

printf(" ->%3d ", v);


visited[v] = 1;
queue[rear] = v;

while (front <= rear) {


v = queue[front]; /* delete from queue */
front++;
for (u = 0; u < SIZE; u++) {
/* Check for adjacent unvisited nodes */
if (!visited[u] && G[v][u] == 1) {
printf(" ->%3d ", u);
visited[u] = 1;
rear++;
queue[rear] = u;
}
}
}
}

3
int main() {
int i, edges, selection, u, v, G[SIZE][SIZE], visited[SIZE];

// intialise graph with 0 values


initGraph(G);

printf("Enter number of edges: ");


scanf("%d", &edges);

for (i = 1; i <= edges; i++) {


printf("Edge %d start node: ", i);
scanf("%d", &u);
printf("Edge %d end node: ", i);
scanf("%d", &v);
insertEdge(u, v, G);
}

do {
printf("\nChoose one of the following:\n");
printf("1. Display graph\n");
printf("2. DFS\n");
printf("3. BFS\n");
printf("0. Exit\n");
scanf("%d", &selection);
switch (selection) {
case 0:
break;
case 1:
printf("Adjacency Matrix\n");
displayGraph(G);
break;
case 2:
printf("Enter starting node for Depth First Search: ");
scanf("%d", &v);
for (i = 0; i < SIZE; i++)
visited[i] = 0;
dfs(G, visited, v);
break;
case 3:
printf("Enter starting node for Breadth First Search : ");
scanf("%d", &v);
for (i = 0; i < SIZE; i++)
visited[i] = 0;
bfs(G, visited, v);
break;
default:
printf("Wrong choice\n");
break;
}
} while (selection != 0);

return 0;
}

You might also like