Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
Graph Class Reference
Collaboration diagram for Graph:
[legend]

Public Member Functions

 Graph (int V, int E)
 
void addEdge (int src, int dst, int weight)
 
 Graph (int V)
 
void addEdge (int src, int dst, int weight)
 
 Graph (Graph &&)=default
 
Graphoperator= (Graph &&)=default
 
 Graph (Graph const &)=default
 
Graphoperator= (Graph const &)=default
 
 Graph (unsigned int vertices, AdjList adjList)
 
 Graph (unsigned int vertices, AdjList &&adjList)
 
 Graph (unsigned int vertices, std::vector< Edge > const &edges)
 
std::remove_reference< AdjList >::type const & getAdjList () const
 
unsigned int getVertices () const
 
void addVertices (unsigned int num=1)
 
void addEdge (Edge const &edge)
 
void addEdge (unsigned int source, unsigned int destination)
 
void set_graph ()
 
void ford_fulkerson ()
 
void print_flow_info ()
 

Public Attributes

int vertexNum
 
int edgeNum
 
Edgeedges
 
int ** edges
 

Private Member Functions

bool bfs (int source, int sink)
 

Private Attributes

unsigned int m_vertices = 0
 
AdjList m_adjList
 
std::vector< std::vector< int > > residual_capacity
 
std::vector< std::vector< int > > capacity
 
int total_nodes = 0
 
int total_edges = 0
 
int source = 0
 
int sink = 0
 
std::vector< int > parent
 
std::vector< std::tuple< int, int, int > > edge_participated
 
std::bitset< MAXN > visited
 
int max_flow = 0
 

Detailed Description

Implementation of graph class.

The graph will be represented using Adjacency List representation. This class contains 2 data members "m_vertices" & "m_adjList" used to represent the number of vertices and adjacency list of the graph respectively. The vertices are labelled 0 - (m_vertices - 1).

Constructor & Destructor Documentation

◆ Graph() [1/6]

Graph::Graph ( int V,
int E )
inline
19 {
20 this->vertexNum = V;
21 this->edgeNum = E;
22 this->edges = (Edge *)malloc(E * sizeof(Edge));
23 }
Definition bellman_ford.cpp:7
T malloc(T... args)

◆ Graph() [2/6]

Graph::Graph ( int V)
inline
16 {
17 this->vertexNum = V;
18 this->edges = new int *[V];
19 for (int i = 0; i < V; i++) {
20 this->edges[i] = new int[V];
21 for (int j = 0; j < V; j++) this->edges[i][j] = INT_MAX;
22 this->edges[i][i] = 0;
23 }
24 }

◆ ~Graph()

Graph::~Graph ( )
inline
26 {
27 for (int i = 0; i < vertexNum; i++) delete[] edges[i];
28 delete[] edges;
29 }

◆ Graph() [3/6]

Graph::Graph ( )
inline
56: m_adjList({}) {}

◆ Graph() [4/6]

Graph::Graph ( unsigned int vertices,
AdjList adjList )
inline

Create a graph from vertices and adjacency list.

Parameters
verticesspecify the number of vertices the graph would contain.
adjListis the adjacency list representation of graph.
69 : m_vertices(vertices), m_adjList(std::move(adjList)) {}
T move(T... args)

◆ Graph() [5/6]

Graph::Graph ( unsigned int vertices,
AdjList && adjList )
inline

Create a graph from vertices and adjacency list.

Parameters
verticesspecify the number of vertices the graph would contain.
adjListis the adjacency list representation of graph.
77 : m_vertices(vertices), m_adjList(std::move(adjList)) {}

◆ Graph() [6/6]

Graph::Graph ( unsigned int vertices,
std::vector< Edge > const & edges )
inline

Create a graph from vertices and a set of edges.

Adjacency list of the graph would be created from the set of edges. If the source or destination of any edge has a value greater or equal to number of vertices, then it would throw a range_error.

Parameters
verticesspecify the number of vertices the graph would contain.
edgesis a vector of edges.
89 : m_vertices(vertices) {
90 for (auto const& edge : edges) {
91 if (edge.src >= vertices || edge.dest >= vertices) {
92 throw std::range_error(
93 "Either src or dest of edge out of range");
94 }
95 m_adjList[edge.src].emplace_back(edge.dest);
96 }
97 }

Member Function Documentation

◆ addEdge() [1/4]

void Graph::addEdge ( Edge const & edge)
inline

Add an edge in the graph.

Parameters
edgethat needs to be added.
124 {
125 if (edge.src >= m_vertices || edge.dest >= m_vertices) {
126 throw std::range_error("Either src or dest of edge out of range");
127 }
128 m_adjList[edge.src].emplace_back(edge.dest);
129 }

◆ addEdge() [2/4]

void Graph::addEdge ( int src,
int dst,
int weight )
inline
26 {
27 static int edgeInd = 0;
28 if (edgeInd < this->edgeNum) {
29 Edge newEdge;
30 newEdge.src = src;
31 newEdge.dst = dst;
32 newEdge.weight = weight;
33 this->edges[edgeInd++] = newEdge;
34 }
35 }

◆ addEdge() [3/4]

void Graph::addEdge ( int src,
int dst,
int weight )
inline
32 {
33 this->edges[src][dst] = weight;
34 }

◆ addEdge() [4/4]

void Graph::addEdge ( unsigned int source,
unsigned int destination )
inline

Add an Edge in the graph

Parameters
sourceis source vertex of the edge.
destinationis the destination vertex of the edge.
136 {
137 if (source >= m_vertices || destination >= m_vertices) {
138 throw std::range_error(
139 "Either source or destination of edge out of range");
140 }
141 m_adjList[source].emplace_back(destination);
142 }

◆ addVertices()

void Graph::addVertices ( unsigned int num = 1)
inline

Add vertices in the graph.

Parameters
numis the number of vertices to be added. It adds 1 vertex by default.
118{ m_vertices += num; }

◆ bfs()

bool Graph::bfs ( int source,
int sink )
inlineprivate
26 { // to find the augmented - path
27 visited.reset();
29 q.push(source);
30 bool is_path_found = false;
31 while (q.empty() == false && is_path_found == false) {
32 int current_node = q.front();
33 visited.set(current_node);
34 q.pop();
35 for (int i = 0; i < total_nodes; ++i) {
36 if (residual_capacity[current_node][i] > 0 && !visited[i]) {
37 visited.set(i);
38 parent[i] = current_node;
39 if (i == sink) {
40 return true;
41 }
42 q.push(i);
43 }
44 }
45 }
46 return false;
47 }
T reset(T... args)
T set(T... args)

◆ ford_fulkerson()

void Graph::ford_fulkerson ( )
inline
62 {
63 while (bfs(source, sink)) {
64 int current_node = sink;
66 while (current_node != source) {
67 int parent_ = parent[current_node];
68 flow = std::min(flow, residual_capacity[parent_][current_node]);
69 current_node = parent_;
70 }
71 current_node = sink;
72 max_flow += flow;
73 while (current_node != source) {
74 int parent_ = parent[current_node];
75 residual_capacity[parent_][current_node] -= flow;
76 residual_capacity[current_node][parent_] += flow;
77 current_node = parent_;
78 }
79 }
80 }
T max(T... args)
T min(T... args)

◆ getAdjList()

std::remove_reference< AdjList >::type const & Graph::getAdjList ( ) const
inline

Return a const reference of the adjacency list.

Returns
const reference to the adjacency list
103 {
104 return m_adjList;
105 }

◆ getVertices()

unsigned int Graph::getVertices ( ) const
inline
Returns
number of vertices in the graph.
110{ return m_vertices; }

◆ print_flow_info()

void Graph::print_flow_info ( )
inline
81 {
82 for (int i = 0; i < total_nodes; ++i) {
83 for (int j = 0; j < total_nodes; ++j) {
84 if (capacity[i][j] &&
85 residual_capacity[i][j] < capacity[i][j]) {
86 edge_participated.emplace_back(std::make_tuple(
87 i, j, capacity[i][j] - residual_capacity[i][j]));
88 }
89 }
90 }
91 std::cout << "\nNodes : " << total_nodes << "\nMax flow: " << max_flow
92 << "\nEdge present in flow: " << edge_participated.size()
93 << '\n';
94 std::cout << "\nSource\tDestination\tCapacity\total_nodes";
95 for (auto& edge_data : edge_participated) {
96 int source = 0, destination = 0, capacity_ = 0;
97 std::tie(source, destination, capacity_) = edge_data;
98 std::cout << source << "\t" << destination << "\t\t" << capacity_
99 << '\t';
100 }
101 }
T emplace_back(T... args)
T make_tuple(T... args)
T size(T... args)
T tie(T... args)

◆ set_graph()

void Graph::set_graph ( )
inline
50 {
51 std::cin >> total_nodes >> total_edges >> source >> sink;
52 parent = std::vector<int>(total_nodes, -1);
53 capacity = residual_capacity = std::vector<std::vector<int> >(
54 total_nodes, std::vector<int>(total_nodes));
55 for (int start = 0, destination = 0, capacity_ = 0, i = 0;
56 i < total_edges; ++i) {
57 std::cin >> start >> destination >> capacity_;
58 residual_capacity[start][destination] = capacity_;
59 capacity[start][destination] = capacity_;
60 }
61 }

The documentation for this class was generated from the following files: