summaryrefslogtreecommitdiff
path: root/graph/euler.cpp
diff options
context:
space:
mode:
authorpjungeblut <paul.jungeblut@gmail.com>2014-11-23 23:01:04 +0100
committerpjungeblut <paul.jungeblut@gmail.com>2014-11-23 23:01:04 +0100
commit3bf9e44bf552ef5ceef2a4eef87907cc1a8db09b (patch)
tree0348c99d32361c5787a41740c0d1f5156a5bd031 /graph/euler.cpp
parent4cc304e57566d149582d974cdaf4a7f724c6b5c1 (diff)
parent213662f659ed8b0a95da110ae6eb5e91e2ecae71 (diff)
gebaut
Diffstat (limited to 'graph/euler.cpp')
-rw-r--r--graph/euler.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/graph/euler.cpp b/graph/euler.cpp
new file mode 100644
index 0000000..74b3399
--- /dev/null
+++ b/graph/euler.cpp
@@ -0,0 +1,41 @@
+vector< vector<int> > adjlist;
+vector< vector<int> > otherIdx;
+vector<int> cycle;
+vector<int> validIdx;
+
+void swapEdges(int n, int a, int b) { // Vertauscht Kanten mit Indizes a und b von Knoten n.
+ int neighA = adjlist[n][a];
+ int neighB = adjlist[n][b];
+ int idxNeighA = otherIdx[n][a];
+ int idxNeighB = otherIdx[n][b];
+ swap(adjlist[n][a], adjlist[n][b]);
+ swap(otherIdx[n][a], otherIdx[n][b]);
+ otherIdx[neighA][idxNeighA] = b;
+ otherIdx[neighB][idxNeighB] = a;
+}
+
+void removeEdge(int n, int i) { // Entfernt Kante i von Knoten n (und die zugehoerige Rueckwaertskante).
+ int other = adjlist[n][i];
+ if (other == n) { //Schlingen
+ validIdx[n]++;
+ return;
+ }
+ int otherIndex = otherIdx[n][i];
+ validIdx[n]++;
+ if (otherIndex != validIdx[other]) {
+ swapEdges(other, otherIndex, validIdx[other]);
+ }
+ validIdx[other]++;
+}
+
+//findet Eulerzyklus an Knoten n startend
+//teste vorher, dass Graph zusammenhaengend ist! (isolierte Punkte sind ok)
+//teste vorher, ob Eulerzyklus ueberhaupt existiert!
+void euler(int n) {
+ while (validIdx[n] < (int)adjlist[n].size()) {
+ int nn = adjlist[n][validIdx[n]];
+ removeEdge(n, validIdx[n]);
+ euler(nn);
+ }
+ cycle.push_back(n); //Zyklus am Ende in cycle
+} \ No newline at end of file