Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions cpp/include/algorithms/graph.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#pragma once
#include <vector>
#include <string>
#include <vector>
#include <unordered_map>

// Forward declarations to avoid circular dependencies
struct GraphNode {
int id;
std::string label;
double x, y;

GraphNode(int id = 0, const std::string& label = "", double x = 0.0, double y = 0.0);
std::string color;

GraphNode(int id, const std::string& label = "", double x = 0.0, double y = 0.0, const std::string& color = "white");
};

struct GraphEdge {
int from, to;
double weight;
bool directed;

GraphEdge(int from = 0, int to = 0, double weight = 1.0, bool directed = false);
std::string color;

GraphEdge(int from, int to, double weight = 1.0, bool directed = false, const std::string& color = "black");
};

struct GraphStep {
Expand All @@ -28,27 +29,24 @@ struct GraphStep {
std::unordered_map<int, double> distances;
std::unordered_map<int, int> parents;
std::string operation;
GraphStep(const std::string& operation = "");

GraphStep(const std::string& operation);
};

class Graph {
private:
std::vector<GraphNode> nodes;
std::vector<GraphEdge> edges;
std::vector<std::vector<int>> adjList;
std::vector<std::vector<std::pair<int, double>>> weightedAdjList;

public:
void addNode(const GraphNode& node);
void addEdge(const GraphEdge& edge);
void buildAdjacencyList();

std::vector<GraphStep> bfs(int start);
std::vector<GraphStep> dfs(int start);
std::vector<GraphStep> dijkstra(int start, int end = -1);
std::vector<GraphStep> aStar(int start, int end);
std::vector<GraphStep> kruskal();
std::vector<GraphStep> prim();
struct SortingStep {
std::vector<int> array;
std::vector<int> highlighted;
std::vector<int> comparing;
std::string operation;
int operations_count;
std::string time_complexity;
std::string space_complexity;

SortingStep(const std::vector<int>& array,
const std::vector<int>& highlighted,
const std::vector<int>& comparing,
const std::string& operation,
int operations_count,
const std::string& time_complexity,
const std::string& space_complexity);
};

Loading