-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.php
More file actions
128 lines (108 loc) · 3.51 KB
/
bfs.php
File metadata and controls
128 lines (108 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
header("Content-Type: application/json");
// Database Connection
$conn = new mysqli("localhost", "root", "", "test");
// Check connection
if ($conn->connect_error) {
http_response_code(500);
echo json_encode(["error" => "Database connection failed: " . $conn->connect_error]);
exit;
}
// Function to check if the graph is empty
function isGraphEmpty($graph) {
return empty($graph);
}
// Function to detect self-loops and log them separately
function detectSelfLoops($graph) {
$selfLoops = [];
foreach ($graph as $node => $neighbors) {
foreach ($neighbors as $neighbor) {
if ($node === $neighbor) {
$selfLoops[] = $node; // Store nodes that have self-loops
}
}
}
return $selfLoops;
}
// Function to perform BFS for a single component while ignoring self-loops in traversal
function bfsComponent($graph, $start, &$visited) {
$queue = [$start];
$result = [];
while (!empty($queue)) {
$node = array_shift($queue);
if (!isset($visited[$node])) {
$visited[$node] = true;
$result[] = $node;
foreach ($graph[$node] as $neighbor) {
if (!isset($visited[$neighbor])) {
$queue[] = $neighbor;
}
}
}
}
return $result;
}
// Function to perform BFS for a disconnected graph
function bfsDisconnected($graph) {
$visited = [];
$components = [];
foreach ($graph as $node => $neighbors) {
if (!isset($visited[$node])) {
$components[] = bfsComponent($graph, $node, $visited);
}
}
return $components;
}
// Handle API request
$data = file_get_contents("php://input");
$data = json_decode($data, true); // Convert JSON object to associative array
// Validate request method
if ($_SERVER['REQUEST_METHOD'] !== "POST") {
http_response_code(405);
echo json_encode(["error" => "Invalid request method. Use POST"]);
exit;
}
// Validate input structure
if (!isset($data["start"]) || !isset($data["graph"])) {
http_response_code(400);
echo json_encode(["error" => "Start node or graph not provided"]);
exit;
}
$startNode = $data["start"];
$graph = $data["graph"];
// Edge case handling
if (isGraphEmpty($graph)) {
http_response_code(400);
echo json_encode(["error" => "Graph is empty"]);
exit;
}
// Detect self-loops
$selfLoops = detectSelfLoops($graph);
// Perform BFS
$bfsResult = bfsDisconnected($graph);
$output = [
"bfs_result" => $bfsResult,
"self_loops" => array_values(array_unique($selfLoops)) // Remove duplicates
];
// Convert JSON data safely for MySQL
$graphJson = mysqli_real_escape_string($conn, json_encode($graph));
$outputJson = mysqli_real_escape_string($conn, json_encode($output));
$algo = "BFS";
// Insert result into database
$sql = "INSERT INTO db_algo (algorithm, input, output) VALUES ('$algo', '$graphJson', '$outputJson')";
if ($conn->query($sql) === TRUE) {
http_response_code(201);
echo json_encode([
"status" => "success",
"message" => "BFS result stored successfully",
"algorithm" => $algo,
"input" => json_decode($graphJson), // Return as array
"output" => json_decode($outputJson) // Return as array
]);
} else {
http_response_code(500);
echo json_encode(["error" => "Error storing BFS result: " . $conn->error]);
}
// Close connection
$conn->close();
?>