Graph Data Structure
Graph Data Structure
Graph
• A graph is a pictorial representation of a set of objects where
some pairs of objects are connected by links.
• The interconnected objects are represented by points termed as
vertices, and the links that connect the vertices are called
edges.
• Formally, a graph is a pair of sets (V, E), where V is the set of
vertices and E is the set of edges, connecting the pairs of
vertices.
Output - S
3. Mark A as visited and put it onto the stack.
• Explore any unvisited adjacent node from A.
• Both S and D are adjacent to A but we are concerned
for unvisited nodes only.
Output – S-A
4. Visit D and mark it visited and put onto the stack.
• Here we have B and C nodes which are
adjacent to D and both are unvisited.
• But we shall again choose in alphabetical
order.
Output – S-A-D
5. We choose B, mark it visited and put onto stack.
• Here B does not have any unvisited adjacent
node. So we pop B from the stack.
Output – S-A-D-B
6. We check stack top for return to previous node and
check if it has any unvisited nodes.
• Here, we find D to be on the top of stack.
Output – S-A-D-B
7. Only unvisited adjacent node is from D is C now.
So we visit C, mark it visited and put it onto the stack.
Output – S-A-D-B-C
Output – S
3. We then see unvisited adjacent node from S.
In this example, we have three nodes but alphabetically
we choose A mark it visited and enqueue it.
Output – S - A
4. Next unvisited adjacent node from S is B.
We mark it visited and enqueue it.
Output – S – A – B
5. Next unvisited adjacent node from S is C.
We mark it visited and enqueue it.
Output – S – A – B – C
6. Now S is left with no unvisited adjacent nodes.
So we dequeue and find A.
7. From A we have D as unvisited adjacent node.
We mark it visited and enqueue it.
Output – S – A – B – C – D