Graph Data Structure
Data Structure Algorithm
Graph Data Structure
• 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 Eis the
set of edges, connecting the pairs of vertices. Take a look at the following graph
• V = {a, b, c, d, e}
• E = {ab, ac, bd, cd, de}
Graph Data Structure
• Vertex − Each node of the graph is represented as a vertex. In the following example, the labeled
circle represents vertices. Thus, A to G are vertices. We can represent them using an array as shown in
the following image. Here A can be identified by index 0. B can be identified using index 1 and so on.
• Edge − Edge represents a path between two vertices or a line between two vertices. In the following
example, the lines from A to B, B to C, and so on represents edges. We can use a two-dimensional
array to represent an array as shown in the following image. Here AB can be represented as 1 at row 0,
column 1, BC as 1 at row 1, column 2 and so on, keeping other combinations as 0.
• Adjacency − Two node or vertices are adjacent if they are connected to each other through an edge.
In the following example, B is adjacent to A, C is adjacent to B, and so on.
• Path − Path represents a sequence of edges between the two vertices. In the following example,
ABCD represents a path from A to D.
Depth First Traversal
• Depth First Search (DFS) algorithm traverses a graph in a depthward motion
and uses a stack to remember to get the next vertex to start a search, when a
dead end occurs in any iteration
Breadth First Traversal
• Breadth First Search (BFS) algorithm traverses a graph in a breadthward
motion and uses a queue to remember to get the next vertex to start a search,
when a dead end occurs in any iteration.
Depth First Traversal
Exp. node OPEN list CLOSED list
{ S } {}
S { A B C } {S}
A { B C D E G } {S A}
B { C D E G G’ } {S A B}
C { D E G G' G" } {S A B C}
D { E G G' G" } {S A B C D}
E { G G' G" } {S A B C D E}
G { G' G" } {S A B C D E}
Breadth First Traversal
exp. node Open list Closed List
{S}
S {A B C} {S}
A {D E G B C} {S A}
D {E G B C} {S A D}
E {G B C} {S A D E}
G {B C} {S A D E}

Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)

  • 1.
    Graph Data Structure DataStructure Algorithm
  • 2.
    Graph Data Structure •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 Eis the set of edges, connecting the pairs of vertices. Take a look at the following graph • V = {a, b, c, d, e} • E = {ab, ac, bd, cd, de}
  • 3.
    Graph Data Structure •Vertex − Each node of the graph is represented as a vertex. In the following example, the labeled circle represents vertices. Thus, A to G are vertices. We can represent them using an array as shown in the following image. Here A can be identified by index 0. B can be identified using index 1 and so on. • Edge − Edge represents a path between two vertices or a line between two vertices. In the following example, the lines from A to B, B to C, and so on represents edges. We can use a two-dimensional array to represent an array as shown in the following image. Here AB can be represented as 1 at row 0, column 1, BC as 1 at row 1, column 2 and so on, keeping other combinations as 0. • Adjacency − Two node or vertices are adjacent if they are connected to each other through an edge. In the following example, B is adjacent to A, C is adjacent to B, and so on. • Path − Path represents a sequence of edges between the two vertices. In the following example, ABCD represents a path from A to D.
  • 4.
    Depth First Traversal •Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration
  • 5.
    Breadth First Traversal •Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and uses a queue to remember to get the next vertex to start a search, when a dead end occurs in any iteration.
  • 6.
    Depth First Traversal Exp.node OPEN list CLOSED list { S } {} S { A B C } {S} A { B C D E G } {S A} B { C D E G G’ } {S A B} C { D E G G' G" } {S A B C} D { E G G' G" } {S A B C D} E { G G' G" } {S A B C D E} G { G' G" } {S A B C D E}
  • 7.
    Breadth First Traversal exp.node Open list Closed List {S} S {A B C} {S} A {D E G B C} {S A} D {E G B C} {S A D} E {G B C} {S A D E} G {B C} {S A D E}