openGPMP
Open Source Mathematics Package
graphs_ex.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <openGPMP/disct.hpp>
3 #include <vector>
4 
5 int main() {
6  gpmp::Graph g(6);
7  g.add_edge(0, 1, 2);
8  g.add_edge(0, 2, 4);
9  g.add_edge(1, 3, 3);
10  g.add_edge(1, 4, 1);
11  g.add_edge(2, 5, 5);
12 
13  std::cout << "DFS Traversal starting from vertex 0: ";
14  g.dfs(0);
15  std::cout << std::endl;
16 
17  std::cout << "BFS Traversal starting from vertex 0: ";
18  g.bfs(0);
19  std::cout << std::endl;
20 
21  g.dijkstra(0);
22 
23  g.bellman_ford(0);
24 
25  std::cout << "Kruskal's Minimum Spanning Tree:\n";
26  std::vector<std::pair<int, std::pair<int, int>>> kruskalMST = g.kruskal();
27  for (const auto &edge : kruskalMST) {
28  std::cout << "Edge: " << edge.second.first << " - "
29  << edge.second.second << " | Weight: " << edge.first << "\n";
30  }
31 
32  g.floyd_warshall();
33 
34  std::cout << "Graph for Topological Sorting:\n";
35  gpmp::Graph dag(6);
36  dag.add_edge(0, 1, 1);
37  dag.add_edge(0, 2, 1);
38  dag.add_edge(1, 3, 1);
39  dag.add_edge(2, 3, 1);
40  dag.add_edge(3, 4, 1);
41  dag.add_edge(4, 5, 1);
42  dag.topo_sort();
43 
44  return 0;
45 }
std::vector< std::pair< int, std::pair< int, int > > > kruskal()
Applies Kruskal's algorithm to find the minimum spanning tree of the graph.
Definition: graphs.cpp:139
void dfs(int start)
Performs Depth-First Search (DFS) traversal starting from a given vertex.
Definition: graphs.cpp:49
void bellman_ford(int start)
Applies Bellman-Ford algorithm to find the single-source shortest paths in the graph,...
Definition: graphs.cpp:116
void add_edge(int v, int w, int weight)
Adds an edge between two vertices with a specified weight.
Definition: graphs.cpp:44
void bfs(int start)
Performs Breadth-First Search (BFS) traversal starting from a given vertex.
Definition: graphs.cpp:65
void floyd_warshall()
Applies Floyd-Warshall algorithm to find all-pairs shortest paths in the graph.
Definition: graphs.cpp:170
void dijkstra(int start)
Applies Dijkstra's algorithm to find the single-source shortest paths in the graph.
Definition: graphs.cpp:86
void topo_sort()
Performs topological sorting on a Directed Acyclic Graph (DAG)
Definition: graphs.cpp:209
int main()
Definition: graphs_ex.cpp:5