Визуализация графика с использованием GraphViz

Обыгрывая теорию графов, я реализовал алгоритм Крускала в C ++ и использовал GraphViz для визуализации, но при запуске кода для генерации файла png с помощью команды:

circo -Tpng PairPQ.cpp -o graph.png

Это показывает это:

Error: PairPQ.cpp:7: syntax error near line 7
context:  >>> using <<<  namespace std;
Warning: non-terminated HTML string starting line 36, file PairPQ.cpp

Я использую Ubuntu Linux для этого и прилагается ниже мой код C ++

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>

**7** using namespace std;

template <typename X, typename Y>

class PairPQ {
pair<X, Y> i;
int r;

public:
PairPQ(const X x, const Y y, const int rank)
: i(x, y),
r(rank) {
}

const pair<X, Y>& item(void) const {
return i;
}

int rank(void) const {
return r;
}

**36** bool operator < (const PairPQ& other) const {
return r < other.r;
}
};

int find_set(const int u, vector<int>& parent) {
// check if at root
if (parent[u] == u)
return u;

// not at root, chase upwards and set parent to ultimate root
return parent[u] = find_set(parent[u], parent);
}

void link_set(const int u,
const int v,
vector<int>& parent,
vector<int>& depth) {
if (depth[u] < depth[v]) {
parent[u] = v;
} else {
parent[v] = u;
if (depth[u] == depth[v])
depth[u]++;
}
}

void union_set(const int u,
const int v,
vector<int>& parent,
vector<int>& depth) {
link_set(find_set(u, parent),
find_set(v, parent),
parent,
depth);
}

int main(int argc, char *argv[]) {
// input number of vertices and edges
cout << "input number of vertices and edges" << endl;
int V, E;
cin >> V >> E;

// check for maximum number of possible edges (disallow anti-parallel arcs)
if (E > V*(V-1)/2) {
E = V*(V-1)/2;
cout << "warning, maximum number of possible edges is " << E << endl;
}

// construct random weighted graph
vector<vector<int> > G(V, vector<int>(V, 0));
for (int i = 0; i < E; i++)
while (true) {
const int u = rand() % V,
v = rand() % V;

// no self loops
if (u != v)
// disallow anti-parallel edges and overwriting existing arcs
if (0 == G[v][u] && 0 == G[u][v]) {
G[u][v] = 1 + rand() % 100;
break;
}
}

// union-find sets are implemented inside the arrays
vector<int> parent(V, 0), depth(V, 0);

// at the start, all nodes point to themselves
for (int i = 0; i < V; i++) {
parent[i] = i;
depth[i] = 0;
}

// load all edges into priority queue, smallest weight on top of max-heap
priority_queue<PairPQ<int, int> > PQ;
for (int u = 0; u < V; u++)
for (int v = 0; v < V; v++)
if (G[u][v])
PQ.push(PairPQ<int, int>(u, v, -G[u][v]));

// keep track of MST edges
set<int> MSTedges;

// loop over all edges in ascending order of weight
while (!PQ.empty()) {
// no need for lazy decrease key idiom here!
const int u = PQ.top().item().first,
v = PQ.top().item().second;
// (don't care about rank/weight, note too that direction does not matter)
PQ.pop();

// check for edge between disjoint sets
if (find_set(u, parent) != find_set(v, parent)) {
MSTedges.insert(u * V + v);
union_set(u, v, parent, depth);
}
}

// print MST graph in GraphViz format
cout << endl
<< "To generate a GraphViz graph from the following, do:" << endl
<< endl
<< "  circo -Tpng file_with_graph_G -o graph.png" << endl
<< endl
<< "-----file_with_graph_G begins here-----" << endl
<< "graph G {" << endl
<< "  node [shape=circle]" << endl;
for (int i = 0; i < V; i++)
for (int j = i+1; j < V; j++) {
const int w = max(G[i][j], G[j][i]);
if (w) {
cout << "  " << i << " -- " << j
<< " [label=\"" << w << "\"";
if (MSTedges.count(i * V + j) || MSTedges.count(j * V + i))
cout << ",color=red";
cout << "];" << endl;
}
}
cout << "}" << endl;

exit(0);
}

Я предполагаю, что мое пространство имен не является неправильным или каким-либо другим способом это сделать?

0

Решение

Из того, что я могу сказать, похоже, что вы должны компилировать эту программу на C ++ с помощью компилятора C ++ (конечно же), такого как GCC, а затем, когда вы запустите программу, она сгенерирует граф GraphViz, который вы можете затем кормить circo, circo понятия не имеет, что делать с вашим файлом C ++.

Что-то вроде этого:

$ g++ PairPQ.cpp -o PairPQ
$ ./PairPQ > MyGraph
$ circo -Tpng MyGraph -o graph.png
1

Другие решения

Других решений пока нет …