-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
681 additions
and
233 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,84 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:visual_graphs/graph_editor/models/graph.dart'; | ||
import 'package:visual_graphs/helpers/queue.dart'; | ||
|
||
class Pair { | ||
final Vertex v; | ||
final Edge? e; | ||
|
||
Pair(this.v, this.e); | ||
} | ||
|
||
class BreadthFirstSearch { | ||
final Graph graph; | ||
final Vertex start; | ||
|
||
final QueueDS<Pair> queue = QueueDS(); | ||
|
||
final Set<Vertex> visited = {}; | ||
final Set<Vertex> seen = {}; | ||
|
||
BreadthFirstSearch({required this.graph, required this.start}); | ||
|
||
void search() async { | ||
print("Starting BFS from ${start.label}"); | ||
await see(Pair(start, null)); | ||
|
||
while (queue.isNotEmpty) { | ||
await Future.delayed(const Duration(seconds: 1)); | ||
|
||
final pair = queue.dequeue(); | ||
await visit(pair); | ||
|
||
var neighbours = pair.v.neighbours; | ||
|
||
for (final neighbour in neighbours.keys) { | ||
if (!seen.contains(neighbour)) { | ||
await see(Pair(neighbour, neighbours[neighbour]!.first)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Future see(Pair pair) async { | ||
print("Seeing ${pair.v.label}"); | ||
|
||
queue.enqueue(pair); | ||
|
||
seen.add(pair.v); | ||
|
||
if (pair.e != null && pair.e?.component != null) { | ||
pair.e?.component | ||
?..color = Colors.orange | ||
..hoverColor = Colors.orangeAccent | ||
..hoverOut(); | ||
|
||
await Future.delayed(const Duration(milliseconds: 100)); | ||
} | ||
|
||
pair.v.component | ||
..color = Colors.orange | ||
..hoverColor = Colors.orangeAccent | ||
..onHoverExit(); | ||
} | ||
|
||
Future visit(Pair pair) async { | ||
print("Visiting ${pair.v.label} via ${pair.e}"); | ||
|
||
visited.add(pair.v); | ||
|
||
if (pair.e != null && pair.e?.component != null) { | ||
pair.e?.component | ||
?..color = Colors.green | ||
..hoverColor = Colors.greenAccent | ||
..hoverOut(); | ||
|
||
await Future.delayed(const Duration(milliseconds: 100)); | ||
} | ||
|
||
pair.v.component | ||
..color = Colors.green | ||
..hoverColor = Colors.greenAccent | ||
..onHoverExit(); | ||
} | ||
} |
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,53 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:visual_graphs/graph_editor/models/graph.dart'; | ||
import 'package:visual_graphs/helpers/stack.dart'; | ||
|
||
class DepthFirstSearch { | ||
final Graph graph; | ||
final Vertex start; | ||
final StackDS<Vertex> stack = StackDS(); | ||
|
||
final Set<Vertex> visited = {}; | ||
final Set<Vertex> seen = {}; | ||
|
||
DepthFirstSearch({required this.graph, required this.start}); | ||
|
||
void search() async { | ||
print("Starting DFS from ${start.label}"); | ||
see(start); | ||
|
||
while (stack.isNotEmpty) { | ||
await Future.delayed(const Duration(seconds: 1)); | ||
|
||
final vertex = stack.pop(); | ||
visit(vertex); | ||
|
||
var neighbours = vertex.neighbours.keys.toList() | ||
..sort((a, b) => a.id.compareTo(b.id)); | ||
|
||
await Future.delayed(const Duration(milliseconds: 100)); | ||
for (final neighbour in neighbours) { | ||
if (!seen.contains(neighbour)) { | ||
see(neighbour); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void see(Vertex vertex) { | ||
print("Seeing ${vertex.label}"); | ||
stack.push(vertex); | ||
seen.add(vertex); | ||
vertex.component.paint = Paint() | ||
..color = Colors.red | ||
..style = PaintingStyle.fill; | ||
} | ||
|
||
void visit(Vertex vertex) { | ||
print("Visiting ${vertex.label}"); | ||
visited.add(vertex); | ||
vertex.component.paint = Paint() | ||
..color = Colors.green | ||
..style = PaintingStyle.fill; | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.