0% found this document useful (0 votes)
5 views

Graph Algorithms (2)

Uploaded by

samueljacso573
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Graph Algorithms (2)

Uploaded by

samueljacso573
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Algorithms for fundamental graph problems

GRAPH ALGORITHMS
What is a Graph?

Ø A graph is a unique data structure in programming that consists of finite sets of


nodes or vertices and a set of edges that connect these vertices to them.
Øadjacent vertices can be called those vertices that are connected to the same edge
with each other.
Øa graph is a visual representation of vertices and edges sharing some connection
or relationship.
Østandard graph algorithms are designed in such a way to solve millions of problems
with just a few lines of logically coded technique.
Types of Graphs

Ø let's look at some types of terms to imply the fundamental variations between
them.
§ Order: Order defines the total number of vertices present in the graph.
§ Size: Size defines the number of edges present in the graph.
§ Self-loop: It is the edges that are connected from a vertex to itself.
§ Isolated vertex: It is the vertex that is not connected to any other vertices in
the graph.
Cont…

§Vertex degree: It is defined as the number of edges incident to a vertex in a


graph.
§ Weighted graph: A graph having value or weight of vertices.
§ Unweighted graph: A graph having no value or weight of vertices.
§ Directed graph: A graph having a direction indicator.
§ Undirected graph: A graph where no directions are defined.
Cont…
Breadth-First Search
Ø breadth-first-search (BFS), you start at a particular vertex, and the algorithm tries to visit all
the neighbors at the given depth before moving on to the next level of traversal of vertices.
Ø Unlike trees, graphs may contain cyclic paths where the first and last vertices are remarkably
the same always.
ØThus, in BFS, you need to keep note of all the track of the vertices you are visiting. To
implement such an order, you use a queue data structure which First-in, First-out approach.
Cont…
Cont…
Algorithm
1. Start putting anyone vertices from the graph at the back of the queue.
2. First, move the front queue item and add it to the list of the visited node.
3. Next, create nodes of the adjacent vertex of that list and add them which
have not been visited yet.
4. Keep repeating steps two and three until the queue is found to be empty.
Pseudocode
Set all nodes to "not visited";
q = new Queue();
q.enqueue(initial node);
while ( q ? empty ) do {
x = q.dequeue();
if ( x has not been visited ) {
visited[x] = true; // Visit node x !
for ( every edge (x, y) /* we are using all edges ! */ )
if ( y has not been visited )
q.enqueue(y); // Use the edge (x,y) !!! }
}
Example 1
Cont…
Cont…
Cont…
Cont…
Cont…
Cont…
Example 2
Depth-first search
Ø Like trees, we traverse all adjacent vertices one by one.
Ø When we traverse an adjacent vertex, we completely finish the traversal of all vertices
reachable through that adjacent vertex.
Ø After we finish traversing one adjacent vertex and its reachable vertices, we move to the next
adjacent vertex and repeat the process.
Ø This is similar to a tree, where we first completely traverse the left subtree and then move to
the right subtree.
ØThe key difference is that, unlike trees, graphs may contain cycles (a node may be visited more
than once). To avoid processing a node multiple times, we use a Boolean visited array.
Example 1
Cont…

Initial state: node 0 is pushed


Cont…
State: after visiting 0
Push the unvisited neighbor nodes: 8, 3, 1
Next, visit the top node in the stack: 1
Cont…
State: after visiting 1
Push the unvisited neighbor nodes: 7
Next, visit the top node in the stack: 7
Cont…

State: after visiting 7


Push the unvisited neighbor nodes: 2
Next, visit the top node in the stack: 2
Cont…
State: after visiting 2
Push the unvisited neighbor nodes: 5, 3
Next, visit the top node in the stack: 3
Cont…

State: after visiting 3


Node 0, already visited
Push the unvisited neighbor nodes: 4
Next, visit the top node in the stack: 4
Cont…

State: after visiting 4


Push the unvisited neighbor nodes: 8
Next, visit the top node in the stack: 8
Cont…

State: after visiting 8


Push the unvisited neighbor nodes: none
Next, visit the top node in the stack: 5
Cont…

State: after visiting 5


Push the unvisited neighbor nodes: 6
Next, visit the top node in the stack: 6
Cont…

State: after visiting 6


Push the unvisited neighbor nodes: none
Next, visit the top node in the stack: 3
3 is visited: skip
Cont…

Next, visit the top node in the stack: 8


8 is visited: skip
Cont…
Result:
Done
§ The stack has become empty
§ and all the nodes of the graph have been traversed.
Cont…
Result:
The printing sequence of the graph will be:

Final path:

§ All nodes are covered by DFS Algorithm


Example 2
Cont…
Cont…
Cont…
Cont…
Cont…

You might also like