I haven't been able to update my blog for a long time because I have been very busy in the last few days, so now here's an update.
I'm going to jump to Eulerian Path in this post. My Program uses a recursive function to get the Eulerian Path for a given graph. For those who don't know what Eulerian Path is
"Eulerian path is a path in a graph which visits each edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex"If there is to be an Eulerian path for a given graph the graph must have lesser than three nodes with odd number of edges. If there are two nodes with odd number of edges the Euler walk must begin in an odd node and end with the other odd node.
This graph can't have an Eulerian Path.

But this graph can have an Eulerian Path.

You can download the eulerwalk.cpp file by clicking on the link.
#include <iostream>
#include <list>
using namespace std;
int graph[100][100];
int n, x, y, steps;
list<int> path;
void walk(int pos){
for(int i = 0; i < n; i++){
if(graph[pos][i] > 0){
graph[pos][i] --;
graph[i][pos] --;
walk(i);
break;
}
}
path.push_back(pos+1);
}
int main(){
cin >> n;
for(int i = 0; i < n; i++){
cin >> x >> y;
graph[x-1][y-1] ++; //we are using zero index
}
walk(0);
while(!path.empty()){
cout << path.back() << ' ';
path.pop_back();
}
}


6 comments:
Thank you very much. Really needed to find a decent C++ code for the Eulerian path. Finally found it :D
Better remove the break if you want this algorithm to work for general graphs.
It is a bit strange that you assume bidirectional connections in walk and enter unidirectional connections in the main.
Hello! I changed it a bit.
Now it should funktion for direkted and undirekted graphs—but I am too short on time now, to test it much.
Nice Greatings from Graz/Austria! Kalten
---SNIPP---
// Original: http://yasith.vidanaarachchi.googlepages.com/eulerwalk.cpp
// http://tuxv.blogspot.com/2007/05/eulerian-path.html
/* a bit altered by Kalten@gmx.at, using note from pieter:
»Better remove the break if you want this algorithm to work for general
graphs.
It is a bit strange that you assume bidirectional connections in walk and
enter unidirectional connections in the main.«
*/
//##############################################################################
#include <iostream>
#include <list>
using namespace std;
#define max_rows 100
int graph[max_rows][max_rows];
int n, x, y, steps, max_node_nr;
bool direkted;
list<int> path;
//##############################################################################
//******************************************************************************
void show_matrix()
{
int o,x,y;
for(y=0; y<max_node_nr ; y++)
{
cout << endl;
for(x=0; x<max_node_nr;x++)
{
o=graph[y][x];
if(!o)
cout << "· ";
else
cout << o << ' ';
}
}
cout << endl;
}
//******************************************************************************
void walk(int pos)
{
int p_i, i_p;
for(int i = 0; i < max_node_nr; i++)
{
p_i=graph[pos][i];
i_p=graph[i][pos];
if(p_i > 0 || i_p > 0)
{
if(p_i>0) graph[pos][i]--;
if(i_p>0) graph[i][pos]--;
show_matrix();
walk(i);
}
}
path.push_back(pos+1);
}
//******************************************************************************
int main()
{
char dir;
cout << "Euler-way with Fleury-Algorithm" << endl;
cout << "from http://tuxv.blogspot.com/2007/05/eulerian-path.html"<<endl<<endl;
cout << "[d]irektet or [u]ndirektet graph? ";
cin >> dir;
switch(dir)
{
case 'd': direkted=true; break;
case 'u': direkted=false; break;
default: cerr << "ERROR: only »d« and »u« accepted!" <<endl;
return 1;
}
cout << "Enter »q« after last edge." << endl;
for(int i = 0; i < max_rows; i++)
{
if(direkted)
cout << i+1 << ". Edge: Startpoint Endpoint: ";
else
cout << i+1 << ". Edge: one-point other-point: ";
if(!(cin >> x >> y))
break;
max_node_nr = (x>max_node_nr) ? x : max_node_nr;
max_node_nr = (y>max_node_nr) ? y : max_node_nr;
graph[x-1][y-1]++; //we are using zero index
if(!direkted) graph[y-1][x-1]++; //we are using zero index
}
show_matrix();
cout << endl << "Path calculation:" <<endl; // debug
walk(0);
cout << endl << "path is: ";
while(!path.empty())
{
cout << path.back() << ' ';
path.pop_back();
}
cout << endl;
}
//******************************************************************************
//eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
---snapp---
i don't understand how to use the code. how does it work and what input should be given?
Warning, none of the posted solutions on this page actually produce a eulerian path, they might in some special cases but it's far from complete. Just wasted a few hours due to this, when I make a correct version I won't post it here.
Hi all, the algorithms discussed here dont give an eulerian path for any general undirected graph.
for example.
2-| |-5
| 1-4 |
3-| |-6
eulerian path should be: 1-2-3-1-4-5-6
first algos output could be(depending on ur input)
1-4-5-6-4-1-3-2-1 which is wrong of course.
Post a Comment