Skip to content

Feature graph implementation #27

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 2 commits into from
Nov 30, 2024
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
78 changes: 41 additions & 37 deletions Headers/0002_Tree/0001_BinarySearchTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,47 @@
#include<vector>
#include<string>
using namespace std;
class Node

namespace BinarySearchTree
{
public:
int data;
Node* parent;
Node* left;
Node* right;
class Node
{
public:
int data;
Node* parent;
Node* left;
Node* right;

Node(int data, Node* parent, Node* left, Node* right);
};
Node(int data, Node* parent, Node* left, Node* right);
};

class BinarySearchTree
{
private:
Node* _root;
void _InsertNode(Node* node);
Node* _FindNode(int value);
Node* _FindMinimumValueNode(Node* node);
Node* _FindMaximumValueNode(Node* node);
Node* _FindSuccessorNode(Node* node);
Node* _FindPredecessorNode(Node* node);
void _Transplant(Node* nodeU, Node* nodeV);
void _DeleteNode(Node* node);
void _RecursiveInorderTraversal(Node* node, vector<int>& result);
void _RecursivePreorderTraversal(Node* node, vector<int>& result);
void _RecursivePostorderTraversal(Node* node, vector<int>& result);
void _MorrisInorderTraversal(Node* node, vector<int>& result);
void _MorrisPreorderTraversal(Node* node, vector<int>& result);
void _MorrisPostorderTraversal(Node* node, vector<int>& result);
public:
BinarySearchTree();
void InsertNode(int value);
void DeleteNode(int value);
vector<int> GetRecursiveInorderTravesalResult();
vector<int> GetRecursivePreorderTravesalResult();
vector<int> GetRecursivePostorderTravesalResult();
vector<int> GetMorrisInorderTraversalResult();
vector<int> GetMorrisPreorderTraversalResult();
vector<int> GetMorrisPostorderTraversalResult();
};
class BinarySearchTree
{
private:
Node* _root;
void _InsertNode(Node* node);
Node* _FindNode(int value);
Node* _FindMinimumValueNode(Node* node);
Node* _FindMaximumValueNode(Node* node);
Node* _FindSuccessorNode(Node* node);
Node* _FindPredecessorNode(Node* node);
void _Transplant(Node* nodeU, Node* nodeV);
void _DeleteNode(Node* node);
void _RecursiveInorderTraversal(Node* node, vector<int>& result);
void _RecursivePreorderTraversal(Node* node, vector<int>& result);
void _RecursivePostorderTraversal(Node* node, vector<int>& result);
void _MorrisInorderTraversal(Node* node, vector<int>& result);
void _MorrisPreorderTraversal(Node* node, vector<int>& result);
void _MorrisPostorderTraversal(Node* node, vector<int>& result);
public:
BinarySearchTree();
void InsertNode(int value);
void DeleteNode(int value);
vector<int> GetRecursiveInorderTravesalResult();
vector<int> GetRecursivePreorderTravesalResult();
vector<int> GetRecursivePostorderTravesalResult();
vector<int> GetMorrisInorderTraversalResult();
vector<int> GetMorrisPreorderTraversalResult();
vector<int> GetMorrisPostorderTraversalResult();
};
}
45 changes: 24 additions & 21 deletions Headers/0003_Graph/0001_BreadthFirstSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@
#include<string>
#include<vector>
using namespace std;
enum color { WHITE, GRAY, BLACK };

class BFSNode
namespace BreadthFirstSearch
{
public:
int data;
int distance;
int color;
BFSNode* parent;
BFSNode(int value);
};
enum color { WHITE, GRAY, BLACK };
class Node
{
public:
int data;
int distance;
int color;
Node* parent;
Node(int value);
};

class BFSGraph
{
private:
map<BFSNode*, list<BFSNode*>> _adjlist;
map<int, BFSNode*> _nodeMap;
BFSNode* MakeOrFindNode(int value);
void BreadthFirstSearch(BFSNode* node);
public:
void PushUndirectedEdge(int valueU, int valueV);
void BFS(int value);
vector<pair<int, int>> ShowBFSResult();
};
class Graph
{
private:
map<Node*, list<Node*>> _adjlist;
map<int, Node*> _nodeMap;
Node* MakeOrFindNode(int value);
void BreadthFirstSearch(Node* node);
public:
void PushUndirectedEdge(int valueU, int valueV);
void BFS(int value);
vector<pair<int, int>> ShowBFSResult();
};
}
50 changes: 27 additions & 23 deletions Headers/0003_Graph/0002_DepthFirstSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,33 @@
#include<string>
#include<vector>
using namespace std;
enum color { WHITE, GRAY, BLACK };

class DFSNode
namespace DepthFirstSearch
{
public:
int data;
int color;
int discoveredTime;
int finishingTime;
DFSNode* parent;
DFSNode(int value);
};
enum color { WHITE, GRAY, BLACK };

class DFSGraph
{
private:
int time;
map<DFSNode*, list<DFSNode*>> _adjlist;
map<int, DFSNode*> _nodeMap;
DFSNode* MakeOrFindNode(int value);
void DepthFirstSearch(DFSNode* DFSNode);
public:
void PushDirectedEdge(int valueU, int valueV);
void DFS();
vector<pair<int,pair<int,int>>> ShowDFSResult();
};
class Node
{
public:
int data;
int color;
int discoveredTime;
int finishingTime;
Node* parent;
Node(int value);
};

class Graph
{
private:
int time;
map<Node*, list<Node*>> _adjlist;
map<int, Node*> _nodeMap;
Node* MakeOrFindNode(int value);
void DepthFirstSearch(Node* Node);
public:
void PushDirectedEdge(int valueU, int valueV);
void DFS();
vector<pair<int, pair<int, int>>> ShowDFSResult();
};
}
56 changes: 30 additions & 26 deletions Headers/0003_Graph/0003_TopologicalSort.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,36 @@
#include<string>
#include<vector>
using namespace std;
enum color { WHITE, GRAY, BLACK };

class TopologicalSortNode
namespace TopologicalSort
{
public:
int data;
int color;
int discoveryTime;
int finishingTime;
TopologicalSortNode* parent;
TopologicalSortNode(int value);
};
enum color { WHITE, GRAY, BLACK };

class TopologicalSortGraph
{
private:
int time;
bool hasCycle;
map<TopologicalSortNode*, list<TopologicalSortNode*>> _adjlist;
map<int, TopologicalSortNode*> _nodeMap;
list<TopologicalSortNode*> _topologicalSortedNodeList;
TopologicalSortNode* MakeOrFindNode(int value);
void DepthFirstSearch(TopologicalSortNode* DFSNode);
public:
void PushDirectedEdge(int valueU, int valueV);
void PushSingleNode(int valueU);
void TopologicalSort();
vector<pair<int, pair<int, int>>> ShowTopologicalSortResult();
};
class Node
{
public:
int data;
int color;
int discoveryTime;
int finishingTime;
Node* parent;
Node(int value);
};

class Graph
{
private:
int time;
bool hasCycle;
map<Node*, list<Node*>> _adjlist;
map<int, Node*> _nodeMap;
list<Node*> _topologicalSortedNodeList;
Node* MakeOrFindNode(int value);
void DepthFirstSearch(Node* DFSNode);
public:
void PushDirectedEdge(int valueU, int valueV);
void PushSingleNode(int valueU);
void TopologicalSort();
vector<pair<int, pair<int, int>>> ShowTopologicalSortResult();
};
}
62 changes: 33 additions & 29 deletions Headers/0003_Graph/0004_StronglyConnectedComponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,39 @@
#include<string>
#include<vector>
using namespace std;
enum color { WHITE, GRAY, BLACK };

class SCCNode
namespace StronglyConnectedComponents
{
public:
int data;
int color;
int discoveryTime;
int finishingTime;
SCCNode* parent;
SCCNode(int value);
};
enum color { WHITE, GRAY, BLACK };

class StronglyConnectedComponentsGraph
{
private:
int time;
map<SCCNode*, list<SCCNode*>> _adjlistG;
map<SCCNode*, list<SCCNode*>> _adjlistT;
map<int, SCCNode*> _nodeMap;
list<SCCNode*> _nodesFinishingTimeOrder;
vector<vector<int>> _allConnectedComponents;
SCCNode* MakeOrFindNode(int value);
void DepthFirstSearchOnGraphG(SCCNode* DFSNode);
void DepthFirstSearchOnGraphT(SCCNode* DFSNode, vector<int>& connectedComponents);
public:
void PushDirectedEdge(int valueU, int valueV);
void PushSingleNode(int valueU);
void DFSOnGraphG();
void DFSOnGraphT();
vector<vector<int>> FindAllStronglyConnectedComponents();
};
class Node
{
public:
int data;
int color;
int discoveryTime;
int finishingTime;
Node* parent;
Node(int value);
};

class Graph
{
private:
int time;
map<Node*, list<Node*>> _adjlistG;
map<Node*, list<Node*>> _adjlistT;
map<int, Node*> _nodeMap;
list<Node*> _nodesFinishingTimeOrder;
vector<vector<int>> _allConnectedComponents;
Node* MakeOrFindNode(int value);
void DepthFirstSearchOnGraphG(Node* DFSNode);
void DepthFirstSearchOnGraphT(Node* DFSNode, vector<int>& connectedComponents);
public:
void PushDirectedEdge(int valueU, int valueV);
void PushSingleNode(int valueU);
void DFSOnGraphG();
void DFSOnGraphT();
vector<vector<int>> FindAllStronglyConnectedComponents();
};
}
Loading