-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cpp
71 lines (60 loc) · 1.5 KB
/
Graph.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
//
// Created by MrPark on 08.02.2019.
//
#include "Graph.h"
void Graph::dfs(int v)
{
used[v] = 1;
for(auto i=1; i <= k; i++)
{
if(g[v][i] < INF && !used[i])
{
dfs(i);
}
}
}
void Graph::dijkstra(int s)
{
shortest_distance.assign(k+1, INF);
shortest_distance[s] = 0;
used.assign(k+1, 0);
for(auto i=1; i<=k; i++)
{
int v = -1;
for(auto j=1; j<=k; j++)
if (!used[j] && (v == -1 || shortest_distance[j] < shortest_distance[v]))
v = j;
if(shortest_distance[v] == INF)
break;
used[v] = 1;
for(auto j=1; j<=k; j++)
if(shortest_distance[v] + g[v][j] < shortest_distance[j])
shortest_distance[j] = shortest_distance[v] + g[v][j];
}
}
void Graph::floydWarshall()
{
shortest_distances.assign(g.begin(), g.end());
for(auto t=1; t<=k; t++)
for(auto i=1; i<=k; i++)
for(auto j=1; j<=k; j++)
shortest_distances[i][j] = min(shortest_distances[i][j], shortest_distances[i][t] + shortest_distances[t][j]);
}
char Graph::isConnected()
{
used.assign(k+1, 0);
for(auto i=1; i <= k; i++) {
used[i] = 0;
}
dfs(1);
for(auto i=1; i <= k; i++)
if(!used[i])
return 0;
return 1;
}
void Graph::setGraph(unsigned long long k, unsigned long long l, vector<vector<int> > &edges)
{
this->k = k;
this->l = l;
this->g.insert(this->g.begin(), edges.begin(), edges.end());
}