Professional Documents
Culture Documents
Program 3
Program 3
Program 3
#include <stdio.h>
#include <stdlib.h>
struct Graph {
int V;
int** adjacency_matrix;
};
return graph;
}
void DFS(struct Graph* graph, int v, int* visited, int* topological_order, int* index) {
visited[v] = 1;
topological_order[(*index)--] = v;
}
free(visited);
free(topological_order);
}
int main() {
int V, E;
printf("Enter the number of vertices: ");
scanf("%d", &V);
return 0;
}
Output:
Q1: What is a digraph? And what is the concept of topological ordering of vertices in a
digraph?
Ans: A digraph, or directed graph, is a graph in which edges have a specific direction,
indicating a one-way relationship between vertices.
A topological ordering is a linear ordering of the vertices in a digraph such that for every
directed edge (u, v), vertex u comes before vertex v in the ordering.
Q4: What is the key property that a digraph must satisfy for a topological ordering to
exist?
Ans: A digraph must be a Directed Acyclic Graph (DAG) for a valid topological ordering to
exist. Cycles in the graph would create contradictions in the ordering.
Q5: What is the time complexity of obtaining a topological ordering using depth-first
search?
Ans: The time complexity is O(V + E), where V is the number of vertices and E is the
number of edges in the graph.
#include <stdio.h>
#include <stdlib.h>
int main() {
int V;
printf("Enter the number of vertices: ");
scanf("%d", &V);
int graph[MAX_VERTICES][MAX_VERTICES];
transitiveClosure(V, graph);
return 0;
}
Output:
Q3: How does Warshall's algorithm represent the graph and its transitive closure?
Ans: Warshall's algorithm uses an adjacency matrix to represent the graph and updates a
separate matrix to represent the transitive closure.
#include <stdio.h>
return dp[n][capacity];
}
int main() {
int n, capacity;
return 0;
}
Output:
Q3: How does Dynamic Programming solve the 0/1 Knapsack problem efficiently?
Ans: Dynamic Programming solves the problem by building a table where each cell
represents the maximum value that can be obtained with a certain weight capacity and a
subset of items. The values are computed iteratively based on previously calculated
subproblems.
Q5: What is the advantage of using Dynamic Programming for the 0/1 Knapsack
problem?
Ans: Dynamic Programming avoids redundant calculations by storing intermediate results,
leading to significant efficiency improvements compared to naive recursive approaches.
#define N 8 // Define the board size (change this value to solve for different N)
return true;
}
int main() {
int board[N][N];
if (solveNQueens(board, 0)) {
printf("Solution found:\n");
printBoard(board);
} else {
printf("No solution exists.\n");
}
return 0;
}
Output:
Solution found:
Q.......
......Q.
....Q...
.......Q
.Q......
...Q....
.....Q..
..Q.....
Viva Questions
Q5: How do you represent the chessboard in code for the N-Queens problem?
Ans: The chessboard can be represented using a 2D array where each cell represents a
square on the board, and the presence of a queen is indicated by a specific value.