forked from Aditya-Experiments/Graph-Algorithm-Visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (121 loc) · 4.65 KB
/
Copy pathscript.js
File metadata and controls
132 lines (121 loc) · 4.65 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
129
130
131
/* Aditya Pandey - Canvas setup and event handling */
const canvas = document.getElementById("graphCanvas");
const ctx = canvas.getContext("2d");
const logDiv = document.getElementById("log");
let nodes = [], edges = [], selectedNode = null;
canvas.addEventListener("click", function(event) {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
let clickedNode = nodes.find(node => Math.hypot(node.x - x, node.y - y) < 20);
if (clickedNode) {
if (selectedNode && selectedNode !== clickedNode) {
let weight = Math.floor(Math.random() * 10) + 1;
edges.push({ from: selectedNode, to: clickedNode, weight });
selectedNode = null;
} else {
selectedNode = clickedNode;
}
} else {
// Create a new node at the adjusted position
const newNode = {
id: nodes.length + 1,
x: x,
y: y
};
nodes.push(newNode);
}
drawGraph();
});
/* Tanmay Sagar - Graph drawing logic and utility functions */
function drawGraph() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw edges with enhanced styling
edges.forEach(edge => {
ctx.beginPath();
ctx.moveTo(edge.from.x, edge.from.y);
ctx.lineTo(edge.to.x, edge.to.y);
ctx.strokeStyle = "#555"; // Darker edge color for better visibility
ctx.lineWidth = 2;
ctx.stroke();
// Draw edge weights with bold font
ctx.fillStyle = "#000"; // Black color for text
ctx.font = "bold 14px Arial";
ctx.fillText(edge.weight, (edge.from.x + edge.to.x) / 2, (edge.from.y + edge.to.y) / 2);
});
// Draw nodes with enhanced styling
nodes.forEach(node => {
ctx.beginPath();
ctx.arc(node.x, node.y, 20, 0, Math.PI * 2);
ctx.fillStyle = "#87CEEB"; // Light blue color for nodes
ctx.fill();
ctx.strokeStyle = "#000"; // Black border for nodes
ctx.lineWidth = 2;
ctx.stroke();
// Draw node labels with bold font
ctx.fillStyle = "#000"; // Black color for text
ctx.font = "bold 16px Arial";
ctx.fillText(node.id, node.x - 5, node.y + 5);
});
}
/* Kartik Bisht - BFS and DFS implementations */
// Add debugging logs to track execution
console.log("Nodes:", nodes);
console.log("Edges:", edges);
function startBFS() {
if (nodes.length === 0) {
logMessage("No nodes available. Please create nodes to start BFS.");
return;
}
console.log("Starting BFS...");
showPopup("<strong>BFS (Breadth-First Search):</strong> BFS is an algorithm for traversing or searching tree or graph data structures. It starts at the root node and explores all neighbors at the present depth before moving on to nodes at the next depth level.");
let queue = [nodes[0]];
let visited = new Set();
logDiv.innerHTML = "<strong>BFS Execution:</strong><br>";
function step() {
console.log("Queue:", queue);
if (queue.length === 0) return;
let node = queue.shift();
if (visited.has(node)) return step();
visited.add(node);
logMessage(`Visiting Node ${node.id}`);
ctx.fillStyle = "yellow";
ctx.beginPath();
ctx.arc(node.x, node.y, 20, 0, Math.PI * 2);
ctx.fill();
// Fix edge filtering by comparing node IDs
edges.filter(e => e.from.id === node.id).forEach(edge => {
if (!visited.has(edge.to)) queue.push(edge.to);
});
setTimeout(step, 500);
}
step();
}
function startDFS() {
if (nodes.length === 0) {
logMessage("No nodes available. Please create nodes to start DFS.");
return;
}
console.log("Starting DFS...");
showPopup("<strong>DFS (Depth-First Search):</strong> DFS is an algorithm for traversing or searching tree or graph data structures. It starts at the root node and explores as far as possible along each branch before backtracking.");
let stack = [nodes[0]];
let visited = new Set();
logDiv.innerHTML = "<strong>DFS Execution:</strong><br>";
function step() {
console.log("Stack:", stack);
if (stack.length === 0) return;
let node = stack.pop();
if (visited.has(node)) return step();
visited.add(node);
logMessage(`Visiting Node ${node.id}`);
ctx.fillStyle = "green";
ctx.beginPath();
ctx.arc(node.x, node.y, 20, 0, Math.PI * 2);
ctx.fill();
edges.filter(e => e.from.id === node.id).forEach(edge => {
if (!visited.has(edge.to)) stack.push(edge.to);
});
setTimeout(step, 500);
}
step();
}