Breadth First and Depth First Spanning Trees

  • Context: MHB 
  • Thread starter Thread starter andrew1
  • Start date Start date
  • Tags Tags
    Depth Trees
Click For Summary
SUMMARY

This discussion focuses on constructing breadth-first and depth-first spanning trees using a graph traversal algorithm. The breadth-first spanning tree utilizes a queue (FIFO) starting from vertex P, adding connected vertices in alphabetical order, while the depth-first spanning tree employs a stack (LIFO) for the same purpose. The final results for the breadth-first tree are confirmed to be QSTUR, aligning with the expected output from the algorithm. Both methods emphasize the importance of maintaining the correct order of vertex connections based on alphabetical precedence.

PREREQUISITES
  • Understanding of graph theory concepts, specifically spanning trees
  • Familiarity with queue and stack data structures
  • Knowledge of breadth-first search (BFS) and depth-first search (DFS) algorithms
  • Basic programming skills to implement the algorithms
NEXT STEPS
  • Implement breadth-first search (BFS) in Python using the queue module
  • Implement depth-first search (DFS) in Python using the stack data structure
  • Explore variations of spanning tree algorithms, such as Prim's and Kruskal's algorithms
  • Study graph traversal complexities and their applications in real-world scenarios
USEFUL FOR

Students, computer science enthusiasts, and software developers interested in graph algorithms and data structure optimization will benefit from this discussion.

andrew1
Messages
20
Reaction score
0
Hi,

I'm currently stuck on how to construct a breadth first and depth first spanning tree for this graph, the algorithm that I'm following is somewhat ambiguous. I'd really appreciate if someone can provide an explanation on how I should go by doing this.

ThanksView attachment 2535
 

Attachments

  • fHhlAH4.jpg
    fHhlAH4.jpg
    56.7 KB · Views: 125
Physics news on Phys.org
Breadth first spanning tree:

In this method we use a queue (FIFO).

You first start with the vertex earliest in the alphabet. Which is vertex P. We add that to the queue.

Step1: queue = P, tree = vertex P

Currently, the front of the queue is P. **

We now have to add the next letter (in alphabetical order) to the end of the queue which is connected to the vertex at the front of the queue (which is P) **.

Step2: queue = PQ, tree = we have a line connecting P and Q

Then we repeat the same process, look for the next letter in alphabetical order, S in this case, which is connected to the vertex at the front of the queue (see ** above) which is still P until there are no other vertices that are connected to P.

So after repeating the steps above we realize that P doesn't connect to anything else. So we take P out of the queue.

We'll then have vertex Q at the start of the queue ***. We again check any vertices (in alphabetical order) connected to Q that is not already in the queue. R is the only one that is not already in the queue.

So we add R at the end of the queue.

Every time we add another vertex at the end of the queue, we draw a line between that and the vertex at the start of the queue.

When all the vertices has been added in the queue, we just need to delete the rest following the first in first out (FIFO) method (delete from vertex at the front of the queue until it's empty).Depth first spanning tree:

We again start at the vertex earliest in the alphabet.

We now use a stack (LIFO).

We push vertex P into the stack.

Step1: stack = P, tree = vertex P

Currently, the top of the stack is P. **

So we need to check which vertex is connected to the vertex at the top of the stack which is P (in alphabetical order). We can see that this is Q. So we push Q into the stack.

Step2: stack = PQ, tree = line connecting P and Q.

Currently, the top of the stack is Q. **

So we now need to check which vertex is connected to the letter at the top of the stack which is Q.

We can see that R is the earliest in the alphabet so we push R into the stack

Step3: stack = PQR, tree = line connecting P and Q, and line connecting Q and R.

Repeat the same process until all the letters has been pushed into the stack.

In the end we get:

Stack = PQRUTS

So we just have to pop everything out of the stack following last in first out (LIFO).

Stack:

PQRUTS
PQRUT
PQRU and so on until the stack is empty.*Assuming we do everything in alphabetical order.
 
Last edited:
Tried this myself the other night and got QSTUR as the last step in my breadth first spanning tree before taking out all the items from the queue, is this correct?
 
Yes, that is also what I got when I implemented the algorithm.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
1K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 65 ·
3
Replies
65
Views
10K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K