0

When traversing an undirected graph using breadth first search that has no parallel edges and self loops, there exists a vertex x with an adjacent vertex y. When processing vertex "x" it sees that y is already discovered/visited and has a parent/discoverer that is not x and

  • case 1) y is processed
  • case 2) y is not processed

What is are sample situations for each of them to happen? and does it say anything about relationship between x and y?

where, Processed vertex means we have already traversed all of its adjacent vertices Discovered node means y has been discovered by atleast one of its parents. Parent means the vertex that first discovered a given vertex.

Heres my thinking. Clearly, y has atleast 2 parent vertices. "x" and atleast one more parent "a".

 a    x
  \  /
    y

and since y is already discovered in both cases (1) and (2)

  • (case 1) is possible because "a" and "x " (parents of y) dont have any common ancestors or x must have been processed before y. Correct??? or not?

  • (case 2) "a" and "x" must have a common ancestor therefore x is being processed before y. Correct???? or not?

Just for reference, heres the implementation of bfs (see the traverse() function) including a main() based on Steven Skiena's "Algorithm Design Manual" book

#include <iostream>
#include <queue>




class edgeNode {
private:
    int y{ -1 };
    edgeNode* next{ nullptr };
public:
    edgeNode(int _y, edgeNode* _node) : y{ _y }, next{ _node }{}
    edgeNode(int _y) : y{ _y }, next(nullptr){}
    edgeNode() : y(0), next(nullptr) {}
    int getY() const { return y; }
    edgeNode* getNext() const { return next; }
    void print() { std::cout << y; }
};



class bGraph {
static  const int MAX_VERTICES = 100;
    edgeNode* edges[MAX_VERTICES];
    bool discovered[MAX_VERTICES];
    bool processed[MAX_VERTICES];
    int parents[MAX_VERTICES];

public:
    bGraph() {
        for (int i = 0; i < MAX_VERTICES; i++) {
            edges[i] = nullptr;
            discovered[i] = false;
            processed[i] = false;
            parents[i] = -1;    
        }
    }
    edgeNode* getEdge(int v) { return edges[v]; }
    void addEdge(int x, int y, bool directed) {
        edgeNode* node = new edgeNode(y, edges[x]);
        edges[x] = node;        
        if (!directed) {
            addEdge(y, x, true);
        }
    }

void traverse(int v) {
            int x, y;           
            std::queue<int> fifo;
            fifo.push(v);
            while (!fifo.empty()) {             
                x = fifo.front();
                fifo.pop();
                edgeNode* node = getEdge(x);
                if (node == nullptr)
                    continue;
                discovered[x] = true;
                std::cout << "\nStart Processing Vertex: " << x << std::endl; 
                while (node != nullptr) {
                    y = node->getY();
                    if (!processed[y] ) {
                        //process edge 
                        std::cout << "(" << x << "," << y << ") ";
                    }
                    if (!discovered[y]) {
                        fifo.push(y);
                        parents[y] = x;
                        discovered[y] = true;
                    }                   
                    node = node->getNext();
                }
                processed[x] = true;
            std::cout << "\nDone Processing Vertex: " << x << std::endl; 
            }
        }

};
int main()
{
bGraph g;
g.addEdge(1,2, false);
g.addEdge(3,1, false);
g.traverse(1);
    return 0;
}
bsobaid
  • 955
  • 1
  • 16
  • 36
  • 1
    How can you declare a parent in an undirected graph? – Abhinav Mathur Jan 01 '21 at 04:55
  • why not? like I said in my post, whichever vertex first discovered the other vertex while traversing the graph using bfs is its parent. You then use the parents to find shortest paths between two vertices. – bsobaid Jan 01 '21 at 22:29
  • To clarify, parent only exists in the context of traversal tree. Not the original graph. – bsobaid Jan 01 '21 at 22:44

1 Answers1

0

It is a BFS. The one which is closer gets noticed first.

Case 1: y is processed : If the shortest path from start node to x passes via y.

Relationship: y is an ancestor of x.

Case 2: y is not processed : If the shortest path from start node to x doesn't pass via y.

Relationship: y is not an ancestor of x.

Case 3: Depends on your implementation whether y gets processed before x.

Relationship: y may or may not be an ancestor of x.

enter image description here

Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57
  • Thanks Shridhar. I see. So the point is all vertices discovered during BFS have some ancestor/descendant relation between them because they are part of a connected graph component. Also, DFS traverses vertices in the order of increasing distance from start node. So the one processed earlier is closer the start vertex and is therefore an ancestor of the vertices processed afterwards. Sounds correct? – bsobaid Jan 01 '21 at 22:43
  • actually, what is said is not entirely correct. Not all vertices are in ancestor/descendent relationship, as there can be cross edges in the bfs traversal tree of a graph. – bsobaid Jan 01 '21 at 22:54
  • @bsobaid I didn't get your point. Can you convey your point using an example? Would be great if you can specifically stick to the statement in the answer which you feel is not correct. – Shridhar R Kulkarni Jan 02 '21 at 04:10