views:

184

answers:

1

6.1.4 Describe an algorithm based on breadth-first search for finding a shortest odd cycle in a graph.

6.3.5 Describe an algorithm based on directed breadth-first search for finding a shortest directed odd cycle in a digraph.

what is most importent is that it must be a directed graph not necessary bfs but must be the shortest directed odd cycle!!!

Question was taken from "Graph Theory" by J.A. Bondy and U.S.R. Murty

thanks in advance!!!

+1  A: 

Running BFS from every node and seeing what paths lead to the starting node should work. Pick the one that has an odd length and is minimum. O(n^2) running time. The same should work for the directed graph version.

I don't know if there's an O(n) solution, but I'd be interested in one myself.

Here's an attempt at solving this with a single DFS. I've only tested it on a few graphs. You have to give it the number of edges and you must have a node labeled 1. It prints the length of the shortest odd cycle in an undirected graph.

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <iostream>

using namespace std;

const int maxn = 100;

void addNode(vector<int> G[], int x, int y)
{
    G[x].push_back(y);
    G[y].push_back(x);
}

void DFS(vector<int> G[], int node, int parent, int pos[], int stack[], int d, int &min)
{
    stack[d] = node;
    if (pos[node] != 0) // we have a cycle
    {
        int t = d - pos[node];
        if (t % 2 == 1 && t < min) // if it's odd and smaller than what we've found so far
        {
            // use what's in stack to get the actual cycle if you want.
            min = t;
        }
        return;
    }
    pos[node] = d;

    for (int i = 0; i < G[node].size(); ++i)
    {
        if (G[node][i] != parent)
        {
            DFS(G, G[node][i], node, pos, stack, d + 1, min);
        }
    }

    pos[node] = 0;
}

int main()
{
    vector<int> G[maxn];
    int pos[maxn]; // position of a node in the stack
    int stack[maxn];

    memset(pos, 0, maxn*sizeof(int));

    int min = (1 << 30);

    // read the edges of the graph.
    int m;
    cin >> m;
    int x, y;
    for (int i = 1; i <= m; ++i)
    {
        cin >> x >> y;
        addNode(G, x, y);
    }

    DFS(G, 1, 0, pos, stack, 1, min); // start DFS from node 1

    cout << min << endl;

    return 0;
}
IVlad
I dont understand how those general guide lines should help me with my solution... yes I do know what cycle is and pick up the odd length and minimum that cant be serious... please if you do have the solution an answer would be nice... this isnt even considered as a hint. how and how is all I say reading your answer :(thanks for the effort anyway...
@gleb-pendler - I'm not sure what you want. I think if you know what BFS is I have made myself very clear. For each node, you run a BFS from that node. You keep track of the distance from the starting node as you go along. If you reach the starting node again: check if the distance is odd: if yes, you have an odd cycle. Check if the distance is shorter than your current global minimum. If it is, update your global minimum. This is a lot more than a hint, so you're going to have to ask more specific questions if you still don't understand. It's not a full solution, but it should get you started
IVlad
how would you find a cycle in using BFS? how would you check it's an odd length cycle? how do you store all the cycles? those are the main question to answer... you didnt say anything new... the problem begins when realization... as far as I see it... it's not even the right direction if you still have the will I can send you the pseudo-code-code for finding an odd length cycle.The problem is that I do know BFS, as well I do know that DFS and not BFS used for finding a cycles :(Let say we do have the algorithm for finding odd cycle lets think of the most efficient way to find the shortest.
A cycle is just a path from a node to the same node. BFS can find paths. To find the length, every time you move to a new node, that new node's length is the node you move from's length + 1. When you reach the starting node again, you have a cycle. You check if it is odd by checking if the length you reached it with is odd. You run such a BFS from **every** node. For each node, you will be able to find all the cycles that node is part of. You can use both BFS and DFS to find cycles, as I have just made clear. If you'd like to, use DFS instead. I don't know if this is optimal, but it works.
IVlad
The pseudocode you have would be nice. I think if you use DFS like you say it's easy to do the same thing in `O(n)` by only running DFS once. I'll look into this too.
IVlad
@IVIad, there is an O(n+m)(1+numcycles) algo for enumerating all cycles. I haven't read the paper, but from there, I suppose you could keep a running minimum cycle track. So not really O(n+m), but close.