-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo.cpp
77 lines (67 loc) · 2.82 KB
/
Demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Demo program for Exercise 2.
* Author: Benjamin Saldman.
*/
#include "Graph.hpp"
#include "Algorithms.hpp"
using ariel::Algorithms;
#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;
int main()
{
ariel::Graph g;
// 3x3 matrix that represents a connected graph.
vector<vector<int>> graph = {
{0, 1, 0},
{1, 0, 1},
{0, 1, 0}};
g.loadGraph(graph); // Load the graph to the object.
g.printGraph(); // Should print: "Graph with 3 vertices and 4 edges."
cout << Algorithms::isConnected(g) << endl; // Should print: "1" (true).
cout << Algorithms::shortestPath(g, 0, 2) << endl; // Should print: 0->1->2.
cout << Algorithms::isContainsCycle(g) << endl; // Should print: "0" (false).
cout << Algorithms::isBipartite(g) << endl; // Should print: "The graph is bipartite: A={0, 2}, B={1}."
// 5x5 matrix that represents a non-connected graph with a cycle.
vector<vector<int>> graph2 = {
{0, 1, 1, 0, 0},
{1, 0, 1, 0, 0},
{1, 1, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0}};
g.loadGraph(graph2); // Load the graph to the object.
g.printGraph(); // Should print: "Graph with 5 vertices and 8 edges."
cout << Algorithms::isConnected(g) << endl; // Should print: "0" (false).
cout << Algorithms::shortestPath(g, 0, 4) << endl; // Should print: "-1" (there is no path between 0 and 4).
cout << Algorithms::isContainsCycle(g) << endl; // Should print: "The cycle is: 0->1->2->0".
cout << Algorithms::isBipartite(g) << endl; // Should print: "0" (false).
// 5x5 matrix that reprsents a connected weighted graph.
vector<vector<int>> graph3 = {
{0, 1, 2, 0, 0},
{1, 0, 3, 0, 0},
{2, 3, 0, 4, 0},
{0, 0, 4, 0, 5},
{0, 0, 0, 5, 0}};
g.loadGraph(graph3); // Load the graph to the object.
g.printGraph(); // Should print: "Graph with 5 vertices and 10 edges."
cout << Algorithms::isConnected(g) << endl; // Should print: "1" (true).
cout << Algorithms::shortestPath(g, 0, 4) << endl; // Should print: 0->2->3->4.
cout << Algorithms::isContainsCycle(g) << endl; // Should print: "0" (false).
cout << Algorithms::isBipartite(g) << endl; // Should print: "0" (false).
// 5x4 matrix that reprsents invalid graph.
vector<vector<int>> graph4 = {
{0, 1, 2, 0},
{1, 0, 3, 0},
{2, 3, 0, 4},
{0, 0, 4, 0},
{0, 0, 0, 5}};
try
{
g.loadGraph(graph4); // Load the graph to the object.
}
catch (const std::invalid_argument &e)
{
cout << e.what() << endl; // Should print: "Invalid graph: The graph is not a square matrix."
}
}