0

In the book I am reading it tells me to choose a vertex with depth 0 but I do not understand how depth is calculated in a graph. enter image description here

Looking at above example, it chooses vertex A as its starting point and explains that it has depth 0. In my understanding it has depth 0 because it has 0 in-degree (no incoming edges).

But what if the graph is un-directed how do we calculate its depth?

If I think of it as a tree where A is the root it seems to me that I assign G to be the root and thus this time G would have depth 0 thus become a starting point.

I've watched lectures, read articles but cannot figure out how to find a starting point in an un-directed graph and for directed graph is my understanding correct (0 depth => 0 in-degree)?

Thanks in advance.

haneulkim
  • 4,406
  • 9
  • 38
  • 80
  • 1
    It depends what problem you are using BFS to solve. – kaya3 Feb 10 '20 at 07:22
  • say I am trying to find shortest path between two vertex – haneulkim Feb 10 '20 at 07:23
  • 3
    Then you would do BFS starting from the vertex you want to find the path from. – kaya3 Feb 10 '20 at 07:24
  • oh lol dump question... what if you want to visit all verticies? – haneulkim Feb 10 '20 at 07:26
  • 1
    Then you would probably choose any vertex to start from, then if you didn't reach all of them, do another BFS starting from any vertex the first one didn't reach, and so on. But the problem of "visiting all vertices" is less well-defined than finding a shortest path, so you might do different things in different cases. – kaya3 Feb 10 '20 at 07:28

1 Answers1

1

No, your understanding is incorrect. Because there is no depth of for graph . In graph we use starting point , it is given by problem setter.

There is no depth in graph .

Let's we take a example :- What is the depth of Node E is your example

--If we follow path A->B->E

then 2

--If we follow path A->C->D->E

then 3

If there is no incoming edge in any node and you didn't choose it then it will not come in traversal . So you choose A as starting point(you said it "depth 0").

And in undirected graph you can choose any node as starting node according your algorithm .

Depth term is use for Tree data structure. Hopeful , Now you understand what I am saying.

And I am able to clear your confusion.

  • There is such a thing as the "BFS tree" of a (connected) graph starting from a given node, which is the set of edges which BFS followed to visit each node for the first time. In this context you can reasonably talk about the depth of nodes in the BFS tree. – kaya3 Feb 10 '20 at 23:20
  • Thanks, so you are saying in undirected graph it doesn't matter which starting point you choose since you will traverse all verticies however in directed graph if it doesn't have any incoming edge it will NOT be traverse if not chosen as a starting point so if I want to traverse all verticies I would choose a starting point as a vertex with no incoming edge, correct? – haneulkim Feb 11 '20 at 04:15