Skip to content

Release #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
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
44 changes: 44 additions & 0 deletions Headers/0003_Graph/0009_SingleSourceShortestPathBellmanFord.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include<map>
#include<vector>
using namespace std;

namespace SingleSourceShortestPathBellmanFord
{
class Node
{
public:
int data;
int distance;
Node* parent;
Node(int data);
};

class Edge
{
public:
Node* nodeU;
Node* nodeV;
int weight;
Edge(Node* nodeU, Node* nodeV, int weight);
};

class Graph
{
private:
map<Node*, vector<Node*>> _adjlist;
map<int, Node*> _nodeMap;
vector<Edge*> _edgeList;
Node* MakeOrFindNode(int data);
void InitializeSingleSource(Node* sourceNode);
void Relax(Edge* edge);
void GetShortestPath(Node* node, vector<int>& path);


public:
void PushDirectedEdge(int valueU, int valueV, int weight);
bool FindSingleSourceShortestPathBellmanFord(int data);
vector<int> GetShortestPathBellmanFord(int data);
};
}
51 changes: 51 additions & 0 deletions Headers/0003_Graph/0010_DirectedAcyclicGraphShortestPath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include<map>
#include<vector>
#include<list>
using namespace std;

namespace DirectedAcyclicGraphShortestPath
{
enum color {WHITE, GRAY, BLACK};

class Node
{
public:
int data;
int color;
int distance;
Node* parent;
Node(int data);
};

class Edge
{
public:
Node* nodeU;
Node* nodeV;
int weight;
Edge(Node* nodeU, Node* nodeV, int weight);
};

class Graph
{
private:
map<Node*, vector<Node*>> _adjlist;
map<int, Node*> _nodeMap;
map<Node*, vector<Edge*>> _edgeMap;
list<Node*> _topologicalSortedNodeList;
Node* MakeOrFindNode(int data);
void DepthFirstSearch(Node* node);
void TopologicalSort();
void InitializeSingleSource(Node* sourceNode);
void Relax(Edge* edge);
void GetShortestPath(Node* node, vector<int>& path);


public:
void PushDirectedEdge(int valueU, int valueV, int weight);
void FindDAGShortestPath(int data);
vector<int> GetDAGShortestPath(int data);
};
}
55 changes: 55 additions & 0 deletions Headers/0003_Graph/0011_SingleSourceShortestPathDijkstra.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include<map>
#include<vector>
#include<set>
using namespace std;

namespace SingleSourceShortestPathDijkstra
{
class Node
{
public:
int data;
int distance;
Node* parent;
Node(int data);
};

class Edge
{
public:
Node* nodeU;
Node* nodeV;
int weight;
Edge(Node* nodeU, Node* nodeV, int weight);
};

class CompareNodeDistance
{
public:
bool operator()(const Node* nodeU, const Node* nodeV) const
{
return nodeU->distance < nodeV->distance;
}
};

class Graph
{
private:
map<Node*, vector<Node*>> _adjlist;
map<int, Node*> _nodeMap;
map<Node*, vector<Edge*>> _edgeMap;
multiset<Node*, CompareNodeDistance> _operationalSet;
Node* MakeOrFindNode(int data);
void InitializeSingleSource(Node* sourceNode);
void Relax(Edge* edge);
void Dijkstra(Node* source);
void GetShortestPath(Node* node, vector<int>& path);

public:
void PushDirectedEdge(int valueU, int valueV, int weight);
void FindShortestPathDijkstra(int data);
vector<int> GetDijkstraShortestPath(int data);
};
}
43 changes: 43 additions & 0 deletions Headers/0003_Graph/0012_DifferenceConstraintsShortestPaths.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include<map>
#include<string>
#include<vector>
using namespace std;

namespace DifferenceConstraintsShortestPaths
{
class Node
{
public:
string data;
int distance;
Node(string data);
};

class Edge
{
public:
Node* nodeU;
Node* nodeV;
int weight;
Edge(Node* nodeU, Node* nodeV, int weight);
};

class Graph
{
private:
map<Node*, vector<Node*>> _adjlist;
map<string, Node*> _nodeMap;
vector<Edge*> _edgeList;
Node* MakeOrFindNode(string data);
void PushDirectedEdge(string valueU, string valueV, int weight);
void InitializeSingleSource(Node* sourceNode);
void Relax(Edge* edge);

public:
void PushAllDirectedEdges(vector<vector<int>> vectorA, vector<string> vectorX, vector<int> vectorB);
bool FindDifferenceConstraintsSolutionBellmanFord();
vector<pair<string, int>> GetDifferenceConstrtaintsSolution();
};
}
2 changes: 1 addition & 1 deletion SourceCodes/0003_Graph/0003_TopologicalSort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace TopologicalSort
this->time++;
nodeU->discoveryTime = this->time;
nodeU->color = GRAY;
for (auto nodeV : this->_adjlist[nodeU])
for (auto& nodeV : this->_adjlist[nodeU])
{
if (nodeV->color == WHITE)
{
Expand Down
108 changes: 108 additions & 0 deletions SourceCodes/0003_Graph/0009_SingleSourceShortestPathBellmanFord.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "../Headers/0003_Graph/0009_SingleSourceShortestPathBellmanFord.h"
#include<climits>
#include<algorithm>
using namespace std;

namespace SingleSourceShortestPathBellmanFord
{
Node::Node(int data)
{
this->data = data;
this->distance = INT_MAX;
this->parent = nullptr;
}

Edge::Edge(Node* nodeU, Node* nodeV, int weight)
{
this->nodeU = nodeU;
this->nodeV = nodeV;
this->weight = weight;
}

// Graph Private Member Methods
Node* Graph::MakeOrFindNode(int data)
{
Node* node = nullptr;
if (this->_nodeMap.find(data) == this->_nodeMap.end())
{
node = new Node(data);
this->_nodeMap[data] = node;
}
else
{
node = this->_nodeMap[data];
}
return node;
}

void Graph :: InitializeSingleSource(Node* sourceNode)
{
for (auto& iterator : this->_nodeMap)
{
iterator.second->distance = INT_MAX;
iterator.second->parent = nullptr;
}
sourceNode->distance = 0;
}

void Graph::Relax(Edge* edge)
{
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)))
{
edge->nodeV->distance = edge->nodeU->distance + edge->weight;
edge->nodeV->parent = edge->nodeU;
}
}

void Graph::GetShortestPath(Node* node, vector<int>& path)
{
path.push_back(node->data);
if (node->parent != nullptr)
{
this->GetShortestPath(node->parent, path);
}
}

// Graph Public Member Methods
void Graph::PushDirectedEdge(int dataU, int dataV, int weight)
{
Node* nodeU = this->MakeOrFindNode(dataU);
Node* nodeV = this->MakeOrFindNode(dataV);

this->_adjlist[nodeU].push_back(nodeV);
this->_edgeList.push_back(new Edge(nodeU, nodeV, weight));
}

bool Graph::FindSingleSourceShortestPathBellmanFord(int data)
{
Node* source = this->_nodeMap[data];

this->InitializeSingleSource(source);

for (int i = 0; i < this->_nodeMap.size() - 1; i++)
{
for (auto& edge : this->_edgeList)
{
this->Relax(edge);
}
}

for (auto& edge : this->_edgeList)
{
if (edge->nodeV->distance > (edge->nodeU->distance + edge->weight))
{
return false;
}
}
return true;
}

vector<int> Graph::GetShortestPathBellmanFord(int data)
{
vector<int> path = {};
Node* node = this->_nodeMap[data];
this->GetShortestPath(node, path);
reverse(path.begin(), path.end());
return path;
}
}
Loading
Loading