Graphs: CSE225: Data Structures and Algorithms
Graphs: CSE225: Data Structures and Algorithms
Graphs
CSE225: Data Structures and Algorithms
Unfinished Business
ClearMarks
Function Sets marks for all vertices to false.
Postcondition All marks have been set to false.
MarkVertex(VertexType vertex)
Function Sets mark for vertex to true.
Precondition vertex is in V(graph).
Postcondition IsMarked(vertex) is true.
Boolean IsMarked(VertexType vertex)
Function Determines if vertex has been marked.
Precondition vertex is in V(graph)
Postcondition Returns true if vertex is marked and false otherwise
Depth-First Search
• Depth-first search is a strategy for exploring a graph and find path
between two vertices
• Explore “deeper” in the graph whenever possible
• Edges are explored out of the most recently discovered vertex v
that still has unexplored edges
• When all of v’s edges have been explored, backtrack to the
vertex from which v was discovered
Depth-First Search
Set found to false
stack.Push(startVertex)
do
stack.Pop(vertex)
if vertex = endVertex
Write final vertex
Set found to true
else if vertex is unvisited
Write this vertex
Push all unvisited adjacent vertices onto stack
while !stack.IsEmpty() AND !found
if(!found)
Write "Path does not exist"
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
F C
A
B D
H
G E
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0]
A [1] B [1]
B [2] C [2]
D
H [3] D [3]
[4] E [4]
G E
[5] F [5]
[6] G [6]
Visited nodes:
[7] H [7]
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0]
A [1] B [1]
B [2] C [2]
D
H [3] D [3]
[4] E [4]
G E
[5] F [5]
[6] G [6]
Visited nodes:
[7] H [7]
Clear the marks (set to false). Push D onto the stack. Set found to false.
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 0
[4] E [4] 0
G E
[5] F [5] 0
[6] G [6] 0
Visited nodes: D
[7] H [7] 0
Clear the marks (set to false). Push D onto the stack. Set found to false.
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 0
[4] E [4] 0
G E
[5] F [5] 0
[6] G [6] 0
Visited nodes: D
[7] H [7] 0
Pop from stack (D is popped). D is not visited yet (unmarked). So, visit D (set D as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 0
[6] G [6] 0
Visited nodes:
D [7] H [7] 0
Pop from stack (D is popped). D is not visited yet (unmarked). So, visit D (set D as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 0
[6] G [6] 0
Visited nodes:
D [7] H [7] 0
Push all the vertices that are adjacent to D and unvisited (unmarked) onto the
stack (C, E and F are pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E F
[5] F [5] 0
E
[6] G [6] 0
Visited nodes: C
D [7] H [7] 0
Push all the vertices that are adjacent to D and unvisited (unmarked) onto the
stack (C, E and F are pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E F
[5] F [5] 0
E
[6] G [6] 0
Visited nodes: C
D [7] H [7] 0
Pop from stack (F is popped). F is not visited yet (unmarked). So, visit F (set F as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1
E
[6] G [6] 0
Visited nodes: C
D F [7] H [7] 0
Pop from stack (F is popped). F is not visited yet (unmarked). So, visit F (set F as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1
E
[6] G [6] 0
Visited nodes: C
D F [7] H [7] 0
Push all the vertices that are adjacent to F and unvisited (unmarked) onto the
stack (C is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E C
[5] F [5] 1
E
[6] G [6] 0
Visited nodes: C
D F [7] H [7] 0
Push all the vertices that are adjacent to F and unvisited (unmarked) onto the
stack (C is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E C
[5] F [5] 1
E
[6] G [6] 0
Visited nodes: C
D F [7] H [7] 0
Pop from stack (C is popped). C is not visited yet (unmarked). So, visit C (set C as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1
E
[6] G [6] 0
Visited nodes: C
D F C [7] H [7] 0
Pop from stack (C is popped). C is not visited yet (unmarked). So, visit C (set C as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1
E
[6] G [6] 0
Visited nodes: C
D F C [7] H [7] 0
Push all the vertices that are adjacent to C and unvisited (unmarked) onto the
stack (nothing is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1
E
[6] G [6] 0
Visited nodes: C
D F C [7] H [7] 0
Pop from stack (E is popped). E is not visited yet (unmarked). So, visit E (set E as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 0
Visited nodes: C
D F C E [7] H [7] 0
Pop from stack (E is popped). E is not visited yet (unmarked). So, visit E (set E as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 0
Visited nodes: C
D F C E [7] H [7] 0
Push all the vertices that are adjacent to E and unvisited (unmarked) onto the
stack (G is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
G
[6] G [6] 0
Visited nodes: C
D F C E [7] H [7] 0
Push all the vertices that are adjacent to E and unvisited (unmarked) onto the
stack (G is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
G
[6] G [6] 0
Visited nodes: C
D F C E [7] H [7] 0
Pop from stack (G is popped). G is not visited yet (unmarked). So, visit G (set G as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes: C
D F C E G [7] H [7] 0
Pop from stack (G is popped). G is not visited yet (unmarked). So, visit G (set G as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes: C
D F C E G [7] H [7] 0
Push all the vertices that are adjacent to G and unvisited (unmarked) onto the
stack (H is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
H
[6] G [6] 1
Visited nodes: C
D F C E G [7] H [7] 0
Push all the vertices that are adjacent to G and unvisited (unmarked) onto the
stack (H is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
H
[6] G [6] 1
Visited nodes: C
D F C E G [7] H [7] 0
Pop from stack (H is popped). H is not visited yet (unmarked). So, visit H (set H as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes: C
D F C E G H [7] H [7] 1
Pop from stack (H is popped). H is not visited yet (unmarked). So, visit H (set H as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes: C
D F C E G H [7] H [7] 1
Push all the vertices that are adjacent to H and unvisited (unmarked) onto the
stack (A and B are pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E B
[5] F [5] 1
A
[6] G [6] 1
Visited nodes: C
D F C E G H [7] H [7] 1
Push all the vertices that are adjacent to H and unvisited (unmarked) onto the
stack (A and B are pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E B
[5] F [5] 1
A
[6] G [6] 1
Visited nodes: C
D F C E G H [7] H [7] 1
Pop from stack (B is popped). B is not visited yet (unmarked). So, visit B (set B as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 1
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
A
[6] G [6] 1
Visited nodes: C
D F C E G H B [7] H [7] 1
Pop from stack (B is popped). B is not visited yet (unmarked). So, visit B (set B as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F false
C [0] A [0] 0
A [1] B [1] 1
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
A
[6] G [6] 1
Visited nodes: C
D F C E G H B [7] H [7] 1
F C
A
B D
H
G E
Depth-First Search
template<class VertexType>
void DepthFirstSearch(GraphType<VertexType> graph,
VertexType startVertex, VertexType endVertex)
{
StackType<VertexType> stack;
QueType<VertexType> vertexQ;
graph.ClearMarks();
stack.Push(startVertex);
do
{
stack.Pop(vertex);
if (vertex == endVertex)
{
cout << vertex);
found = true;
}
Depth-First Search
else
{
if (!graph.IsMarked(vertex))
{
graph.MarkVertex(vertex);
cout << vertex;
graph.GetToVertices(vertex, vertexQ);
while (!vertexQ.IsEmpty())
{
vertexQ.Dequeue(item);
if (!graph.IsMarked(item))
stack.Push(item);
}
}
}
} while (!stack.IsEmpty() && !found);
if (!found)
cout << "Path not found." << endl;
}
Breadth-First Search
• Breadth-first search is a strategy for exploring a graph and find path
between two vertices
• Explore “level by level” in the graph
Breadth-First Search
F
Breadth-first search starts
C
with given node
A
B D
H
0
G E
F
Breadth-first search starts
C
with given node
A
Then visits nodes adjacent in
B D some specified order (e.g.,
H
0 alphabetical)
G E Like ripples in a pond
1
Nodes visited: D
Breadth-First Search
F
Breadth-first search starts
C
with given node
A
Then visits nodes adjacent in
B D some specified order (e.g.,
H
0 alphabetical)
G E Like ripples in a pond
1
Nodes visited: D, C
Breadth-First Search
F
Breadth-first search starts
C
with given node
A
Then visits nodes adjacent in
B D some specified order (e.g.,
H
0 alphabetical)
G E Like ripples in a pond
1
Nodes visited: D, C, E
Breadth-First Search
F
Breadth-first search starts
C
with given node
A
Then visits nodes adjacent in
B D some specified order (e.g.,
H
0 alphabetical)
G E Like ripples in a pond
1
Nodes visited: D, C, E, F
Breadth-First Search
F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
G E
2 1
Nodes visited: D, C, E, F
Breadth-First Search
F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
G E
2 1
Nodes visited: D, C, E, F, G
Breadth-First Search
F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
3 G E
2 1
Nodes visited: D, C, E, F, G
Breadth-First Search
F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
3 G E
2 1
Nodes visited: D, C, E, F, G, H
Breadth-First Search
4 F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H
Breadth-First Search
4 F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H, A
Breadth-First Search
4 F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H, A, B
Breadth-First Search
Set found to false
queue.Enqueue(startVertex)
do
queue.Dequeue (vertex)
if vertex = endVertex
Write final vertex
Set found to true
else if vertex is unvisited
Write this vertex
Enqueue all unvisited adjacent vertices onto queue
while !queue.IsEmpty() AND !found
if(!found)
Write "Path does not exist"
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
F C
A
B D
H
G E
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0]
A [1] B [1]
B [2] C [2]
D
H [3] D [3]
[4] E [4]
G E
[5] F [5]
[6] G [6]
Visited nodes:
[7] H [7]
Enqueue all the vertices that are adjacent to D and unvisited (unmarked) (C, E and
F are enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E F
[5] F [5] 0
E
[6] G [6] 0
Visited nodes: C
D [7] H [7] 0
Enqueue all the vertices that are adjacent to D and unvisited (unmarked) (C, E and
F are enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E F
[5] F [5] 0
E
[6] G [6] 0
Visited nodes: C
D [7] H [7] 0
Enqueue all the vertices that are adjacent to C and unvisited (unmarked) (nothing
is enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 0
F
[6] G [6] 0
Visited nodes: E
D C [7] H [7] 0
Enqueue all the vertices that are adjacent to E and unvisited (unmarked) (G is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 0
G
[6] G [6] 0
Visited nodes: F
D C E [7] H [7] 0
Enqueue all the vertices that are adjacent to E and unvisited (unmarked) (G is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 0
G
[6] G [6] 0
Visited nodes: F
D C E [7] H [7] 0
Enqueue all the vertices that are adjacent to F and unvisited (unmarked) (nothing
is enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 0
Visited nodes: G
D C E F [7] H [7] 0
Enqueue all the vertices that are adjacent to G and unvisited (unmarked) (H is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes: H
D C E F G [7] H [7] 0
Enqueue all the vertices that are adjacent to G and unvisited (unmarked) (H is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes: H
D C E F G [7] H [7] 0
Enqueue all the vertices that are adjacent to H and unvisited (unmarked) (A and B
are enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
B
[6] G [6] 1
Visited nodes: A
D C E F G H [7] H [7] 1
Enqueue all the vertices that are adjacent to H and unvisited (unmarked) (A and B
are enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 0
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
B
[6] G [6] 1
Visited nodes: A
D C E F G H [7] H [7] 1
Enqueue all the vertices that are adjacent to A and unvisited (unmarked) (B is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 1
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
B
[6] G [6] 1
Visited nodes: B
D C E F G H A [7] H [7] 1
Enqueue all the vertices that are adjacent to A and unvisited (unmarked) (B is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F false
C [0] A [0] 1
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
B
[6] G [6] 1
Visited nodes: B
D C E F G H A [7] H [7] 1
F C
A
B D
H
G E
Breadth-First Search
template<class VertexType>
void BreadthFirstSearch(GraphType<VertexType> graph,
VertexType startVertex, VertexType endVertex)
{
QueType<VertexType> queue;
QueType<VertexType> vertexQ;
graph.ClearMarks();
queue.Enqueue(startVertex);
do
{
queue.Dequeue(vertex);
if (vertex == endVertex)
{
cout << vertex;
found = true;
}
Breadth-First Search
else
{
if (!graph.IsMarked(vertex))
{
graph.MarkVertex(vertex);
cout << vertex;
graph.GetToVertices(vertex, vertexQ);
while (!vertexQ.IsEmpty())
{
vertexQ.Dequeue(item);
if (!graph.IsMarked(item))
queue.Enqueue(item);
}
}
}
} while (!queue.IsEmpty() && !found);
if (!found)
cout << "Path not found." << endl;
}
Concluding Remarks
There can be multiple paths from source vertex to destination
vertex.
Both Breadth First Search and Depth First Search ensures that
the path will be found if there is any.
Breadth First Search ensures that, if there are multiple paths
from source vertex to destination vertex, the destination vertex
is reached using minimum number of edges, i.e. it takes the
shortest among all the paths, given that,
The edges do not have weights, OR
All the edges have equal weights
Depth First Search does not ensure that the shortest path is
taken.