Professional Documents
Culture Documents
CODE:
#include <stdio.h>
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
int main() {
int vertices, edges;
return 0;
}
ALGORITHM:
// Main function
function main():
// Declare local variables
int vertices, edges
// End of program
OUTPUT:
Therefore, the overall time complexity of creating and representing a graph using an
adjacency matrix is O(V^2).
2. Space Complexity:
- The space complexity is determined by the size of the adjacency matrix, which is V x V.
- Hence, the space complexity is O(V^2), where V is the number of vertices.
2. Write a C program for implementing breadth first search (BFS) in a graph.
CODE:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 50
// No. of vertices
int V;
bool adj[MAX_VERTICES][MAX_VERTICES];
} Graph;
// Constructor
Graph* Graph_create(int V)
{
Graph* g = malloc(sizeof(Graph));
g->V = V;
return g;
}
// Destructor
void Graph_destroy(Graph* g) { free(g); }
// Driver code
int main()
{
// Create a graph
Graph* g = Graph_create(4);
Graph_addEdge(g, 0, 1);
Graph_addEdge(g, 0, 2);
Graph_addEdge(g, 1, 2);
Graph_addEdge(g, 2, 0);
Graph_addEdge(g, 2, 3);
Graph_addEdge(g, 3, 3);
return 0;
}
ALGORITHM:
Graph Structure:
Structure Graph:
Integer V
Boolean adj[MAX_VERTICES][MAX_VERTICES]
Graph Creation:
Function Graph_create(V):
graph = Allocate memory for Graph structure
graph.V = V
For i from 0 to V-1:
For j from 0 to V-1:
graph.adj[i][j] = false
Return graph
Graph Destruction:
Function Graph_destroy(graph):
Deallocate memory for graph
Adding an Edge:
Function Graph_addEdge(graph, v, w):
Set graph.adj[v][w] = true
BFS Traversal:
Function Graph_BFS(graph, s):
Array visited[MAX_VERTICES]
For i from 0 to graph.V-1:
visited[i] = false
Queue queue[MAX_VERTICES]
Front = 0, Rear = 0
visited[s] = true
Enqueue(queue, s)
Main Function:
Function main():
graph = Graph_create(4)
Graph_addEdge(graph, 0, 1)
Graph_addEdge(graph, 0, 2)
Graph_addEdge(graph, 1, 2)
Graph_addEdge(graph, 2, 0)
Graph_addEdge(graph, 2, 3)
Graph_addEdge(graph, 3, 3)
Graph_destroy(graph)
Return 0
OUTPUT:
DISCUSSION:
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).
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
return graph;
}
free(visited);
}
int main() {
int vertices = 4;
struct Graph* graph = createGraph(vertices);
addEdge(graph, 2, 0);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 0, 1);
addEdge(graph, 3, 3);
addEdge(graph, 1, 3);
return 0;
}
ALGORITHM:
1. **Structures:**
- `struct Node`: Represents a node in the adjacency list.
- `struct List`: Represents the adjacency list for a vertex.
- `struct Graph`: Represents the graph containing an array of adjacency lists.
2. **Functions:**
- `createNode`: Creates a new node with the given data.
- `createGraph`: Creates a new graph with a specified number of vertices.
- `addEdge`: Adds an edge between two vertices in the graph.
- `DFS`: Performs Depth First Search from a given vertex using recursion.
- `DFSTraversal`: Performs DFS traversal from a given set of vertices in a specified order.
3. **Main Function:**
- Creates a graph with 4 vertices.
- Adds edges to the graph using the `addEdge` function.
- Defines an order of vertices for DFS traversal.
- Calls `DFSTraversal` function to perform DFS starting from vertex 2.
4. **Output:**
- The program prints the Depth First Traversal starting from vertex 2 based on the
specified order.
OUTPUT:
DISCUSSION:
For each vertex, DFS visits all its adjacent vertices recursively. In the worst case, DFS may
visit all vertices and edges in the graph. Therefore, the time complexity of DFS is O(V + E),
where V represents the number of vertices and E represents the number of edges in the
graph.