Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
chore(code): remove solutions for only code
Browse files Browse the repository at this point in the history
  • Loading branch information
storopoli committed Sep 29, 2024
1 parent b4ffa80 commit 1e5ff06
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 569 deletions.
13 changes: 11 additions & 2 deletions code/01-graphs-adjacency-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ void print_adj_list(int arr[5][5]) {
// Initialize to all zeros
int adj_list[5][5] = {{0}};

// Traverse the given matrix
// FIXME: add the necessary code here
for (int i = 0; i < 5; ++i) {
printf("%d: ", i + 1);
for (int j = 0; j < 5; ++j) {
if (arr[i][j] == 1) {
adj_list[i][adj_list[i][0]] = j; // Store the neighbor in the list
printf("%d ", j + 1);
adj_list[i][0]++; // Increment the count of neighbors for this vertex
}
}
printf("\n");
}
}

/** Main Function */
Expand Down
22 changes: 17 additions & 5 deletions code/01-graphs-adjacency-matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ int N, M;

/** Function to create an adjacency matrix */
void create_adj_matrix(int adj[][N + 1], int arr[][2]) {
// Initialize all value to zero
// FIXME: add the necessary code here
// Initialize all values to zero
for (int i = 0; i < N + 1; i++) {
for (int j = 0; j < N + 1; j++) {
adj[i][j] = 0;
}
}

// Traverse the array of edges and update the values
// FIXME: add the necessary code here
// Traverse the array of edges
for (int i = 0; i < M; i++) {
// Find X and Y of edges
int x = arr[i][0];
int y = arr[i][1];

// Update value to 1
adj[x][y] = 1;
adj[y][x] = 1;
}
}

/** Function to print an adjacency matrix */
Expand All @@ -24,7 +36,7 @@ void print_adj_matrix(int adj[][N + 1]) {
}
}

/** Main function */
/** Main Function */
int main() {
// Number of vertices
N = 5;
Expand Down
64 changes: 52 additions & 12 deletions code/02-graphs-eulerian-path.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,73 @@ void find_path(int graph[N][N]) {
int path_length = 0;

// Find out number of edges each vertex has
// FIXME: Add the code here
for (int i = 0; i < N; i++) {
numofadj[i] = 0;
for (int j = 0; j < N; j++) {
numofadj[i] += graph[i][j];
}
}

// Find out how many vertex has odd number edges
// FIXME: Add the code here
int startpoint = 0, numofodd = 0;
for (int i = N - 1; i >= 0; i--) {
if (numofadj[i] % 2 == 1) {
numofodd++;
startpoint = i;
}
}

// If number of vertex with odd number of edges is greater than two return "No
// Solution".
// FIXME: Add the code here
if (numofodd > 2) {
printf("No Solution\n");
return;
}

// If there is a path find the path
// Initialize empty stack and path take the starting current as discussed
// FIXME: Add the code here
int cur = startpoint;

// Loop will run until there is element in the stack or current edge has some
// neighbour.
// FIXME: Add the code here

// If the current vertex has at least one neighbour add the current vertex
// to stack, remove the edge between them and set the current to its
// neighbour.
// FIXME: Add the code here
while (top > 0 || numofadj[cur] > 0) {
// If current node has not any neighbour add it to path and pop stack set
// new current to the popped element
if (numofadj[cur] == 0) {
path[path_length++] = cur;
if (top > 0) {
cur = stack[--top];
} else {
break;
}
}

// If the current vertex has at least one neighbour add the current vertex
// to stack, remove the edge between them and set the current to its
// neighbour.
else {
for (int i = 0; i < N; i++) {
if (graph[cur][i] == 1) {
stack[top++] = cur;
graph[cur][i] = 0;
graph[i][cur] = 0;
numofadj[cur]--;
numofadj[i]--;
cur = i;
break;
}
}
}
}

// Add the last vertex to the path
// FIXME: Add the code here
path[path_length++] = cur;

// Print the path
// FIXME: Add the code here
for (int i = path_length - 1; i >= 0; i--) {
printf("%d -> ", path[i]);
}
printf("\n");
}

/** Main function */
Expand Down
67 changes: 65 additions & 2 deletions code/02-graphs-hamiltonian-cycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,76 @@ void print_solution(int path[]) {
printf("\n");
}

/** A utility function to check if the vertex v can be added at index
* 'pos' in the Hamiltonian cycle constructed so far
* (stored in 'path[]')
*/
int is_safe(int v, int graph[N][N], int path[], int pos) {
// Check if this vertex is an adjacent vertex of the previously added vertex.
if (graph[path[pos - 1]][v] == 0) return 0;

// Check if the vertex has already been included.
// This step can be optimized by creating an array of size N
for (int i = 0; i < pos; i++)
if (path[i] == v) return 0;

return 1;
}

/** A recursive utility function to solve Hamiltonian cycle problem */
int hamiltonian_cycle_util(int graph[N][N], int path[], int pos) {
// base case: If all vertices are included in Hamiltonian cycle
if (pos == N) {
// And if there is an edge from the last included vertex to the
// first vertex
if (graph[path[pos - 1]][path[0]] == 1)
return 1;
else
return 0;
}

// Try different vertices as a next candidate in Hamiltonian cycle.
// We don't try for 0 as we included 0 as starting point in
// hamiltonian_cycle()
for (int v = 1; v < N; v++) {
// Check if this vertex can be added to Hamiltonian cycle
if (is_safe(v, graph, path, pos)) {
path[pos] = v;

// recur to construct rest of the path
if (hamiltonian_cycle_util(graph, path, pos + 1) == 1) return 1;

// If adding vertex v doesn't lead to a solution, then remove it
path[pos] = -1;
}
}

// If no vertex can be added to Hamiltonian Cycle constructed so far,
// then return false
return 0;
}

/** This function solves the Hamiltonian cycle problem using Backtracking.
* It mainly uses hamiltonian_cycle_util() to solve the problem.
* It returns false if there is no Hamiltonian Cycle possible,
* otherwise return true and prints the path.
*/
int hamiltonian_cycle(int graph[N][N]) {
// FIXME: Implement the function
// print_solution(path);
int path[N];
for (int i = 0; i < N; i++) path[i] = -1;

// Let us put vertex 0 as the first vertex in the path.
// If there is a Hamiltonian cycle,
// then the path can be started from any point
// of the cycle as the graph is undirected
path[0] = 0;
if (hamiltonian_cycle_util(graph, path, 1) == 0) {
printf("Solution does not exist\n");
printf("\n");
return 0;
}

print_solution(path);
return 1;
}

Expand Down
13 changes: 10 additions & 3 deletions code/03-tree-detection.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
int search_cycle(int graph[N][N], int v, int *visited, int parent) {
visited[v] = 1;
for (int i = 0; i < N; ++i) {
// FIXME: Implement the function
if (graph[v][i]) {
if (!visited[i]) {
if (search_cycle(graph, i, visited, v)) return 1;
} else if (i != parent) { // cycle detected
return 1;
}
}
}
return 0;
}
Expand All @@ -40,10 +46,11 @@ int is_tree(int graph[N][N]) {

// Perform depth-first search from vertex 0 to check if all vertices
// are reachable (connected)
// FIXME: Implement the function
has_cycle = search_cycle(graph, 0, visited, -1);
if (has_cycle) return 0;

// Check if all vertices have been visited
// FIXME: Implement the function
if (!all_visited(visited)) return 0;

// Return true if no cycle is found and all
return 1;
Expand Down
15 changes: 14 additions & 1 deletion code/04-subset_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@
* @return 1 if there is a subset that sums to the target, 0 otherwise
*/
int subset_sum(int set[], int n, int target) {
// FIXME: Implement the subset sum algorithm
// Iterate through all possible subsets
for (int i = 0; i < (1 << n); i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
// Verify if the j-th element is in the subset
if (i & (1 << j)) {
sum += set[j];
}
}
// If the subset sums to the target, return 1
if (sum == target) {
return 1;
}
}
return 0;
}

Expand Down
18 changes: 17 additions & 1 deletion code/05-knapsack.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@ int max(int a, int b) { return (a > b) ? a : b; }
* @return The maximum value that can be obtained with the given capacity.
*/
int knapsack(int W, int weights[], int values[], int n) {
// FIXME: Implement the knapsack algorithm
// Base case: no items left or no capacity left
if (n == 0 || W == 0) {
return 0;
}

// If the weight of the nth item is more than the remaining capacity, it
// cannot be included
if (weights[n - 1] > W) {
return knapsack(W, weights, values, n - 1);
} else {
// Return the maximum of two cases:
// 1. nth item included
// 2. nth item not included
return max(
values[n - 1] + knapsack(W - weights[n - 1], weights, values, n - 1),
knapsack(W, weights, values, n - 1));
}
}

/**
Expand Down
32 changes: 0 additions & 32 deletions solutions/01-graphs-adjacency-list.c

This file was deleted.

57 changes: 0 additions & 57 deletions solutions/01-graphs-adjacency-matrix.c

This file was deleted.

Loading

0 comments on commit 1e5ff06

Please sign in to comment.