-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3-SAT Signed-off-by: Josef Edwards <joed6834@colorado.edu>
- Loading branch information
1 parent
744f42c
commit 166d377
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
// Function to generate a random 3-SAT instance | ||
void generate_3sat_instance(int num_variables, int num_clauses, const char* filename) { | ||
FILE *file = fopen(filename, "w"); | ||
if (!file) { | ||
perror("Failed to open file for writing"); | ||
return; | ||
} | ||
|
||
// Write the header for the 3-SAT CNF instance | ||
fprintf(file, "p cnf %d %d\n", num_variables, num_clauses); | ||
|
||
// Randomly generate clauses | ||
srand(time(0)); // Seed the random number generator | ||
|
||
for (int i = 0; i < num_clauses; ++i) { | ||
// Each clause consists of 3 literals, choose them randomly | ||
for (int j = 0; j < 3; ++j) { | ||
int var = rand() % num_variables + 1; // Variable between 1 and num_variables | ||
if (rand() % 2 == 0) { | ||
var = -var; // Randomly negate the variable | ||
} | ||
fprintf(file, "%d ", var); | ||
} | ||
fprintf(file, "0\n"); // End of clause | ||
} | ||
|
||
fclose(file); | ||
printf("Generated random 3-SAT instance: %s\n", filename); | ||
} |