0% found this document useful (0 votes)
4 views41 pages

CENG205 W-11b

Uploaded by

caglarrahmett
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views41 pages

CENG205 W-11b

Uploaded by

caglarrahmett
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

CENG 205 – Data Structures and Algorithms

Week 11-b - Graphs

Dr. Yücel Tekin


2.2 CLASSIFICATION OF DATA STRUCTURES

Data structure

Primitive (basic) Data structure Non-Primitive Data Structure


Integer
Real
Character Linear Non-Linear
Boolean Arrays Trees
Linked lists Graphs
• fundamental data types supported by a
programming language Stacks • not stored in a sequential order
Queues
• the elements of a data structure are stored
in a linear or sequential order
Chapter 13 : Graphs

In this chapter, we will discuss another non-linear data structure called graphs.

 The concept and the terminology 


 the representation of graphs in the memory 
 the operations that can be performed on them
 Last but not the least, the real-world applications of graphs.
13.6 GRAPH TRAVERSAL ALGORITHMS

 We will discuss how to traverse graphs.


 By traversing a graph, we mean the method of examining the nodes and edges of the graph.

 There are two standard methods of graph traversal:


1. Breadth-first search
2. Depth-first search

 As an auxiliary data structure to store nodes for further processing;


 Breadth-first search uses a queue
 Depth-first search uses a stack

 But both these algorithms make use of a variable STATUS.


 During the execution of the algorithm, every node in the graph will have the variable STATUS set to 1 or 2, depending on
its current state.
13.6.1 Breadth-First Search Algorithm

 Breadth-first search (BFS) is a graph search algorithm

 Begins at the root node and explores all the neighbouring nodes.

 Then for each of those nearest nodes, the algorithm explores


their unexplored neighbour nodes, and so on, until it finds the
goal.
13.6.1 Breadth-First Search Algorithm

 We start examining the node A and then all the neighbours of A are
examined.

 In the next step, we examine the neighbours of neighbours of A, so on


and so forth.

 This is accomplished by using a queue that will hold the nodes that are
waiting for further processing and a variable STATUS to represent the
current state of the node.
Example 13.1

 Consider the graph G and the adjacency list are given in Figure
 Assume that G represents the daily flights between different cities
 We want to fly from city A to I with minimum stops.
 That is, find the minimum path P from A to I given that every edge has a length of 1.
Solution

 The minimum path P can be found by applying the breadth-


first search algorithm that begins at city A and
 ends when I is encountered.

 During the execution of the algorithm, we use two arrays:


QUEUE and ORIG.

 QUEUE is used to hold the nodes that have to be processed


 ORIG is used to keep track of the origin of each edge

 Initially, FRONT = REAR = –1.


Solution

1 - Add A to QUEUE and add NULL to ORIG.

2 - Dequeue a node by setting FRONT = FRONT + 1 (remove the FRONT element


of QUEUE) and enqueue the neighbours of A. Also, add A as the ORIG of its
neighbours.
Solution

3 - Dequeue a node by setting FRONT = FRONT + 1 and enqueue the


neighbours of B. Also, add B as the ORIG of its neighbours.

4 - Dequeue a node by setting FRONT = FRONT + 1 and enqueue the


neighbours of C. Also, add C as the ORIG of its neighbours. Note that C has
two neighbours B and G. Since B has already been added to the queue and it
is not in the Ready state, we will not add B and only add G.
Solution

5 - Dequeue a node by setting FRONT = FRONT + 1 and enqueue the


neighbours of D. Also, add D as the ORIG of its neighbours. Note that D
has two neighbours C and G. Since both of them have already been added
to the queue and they are not in the Ready state, we will not add them
again.

6 - Dequeue a node by setting FRONT = FRONT + 1 and enqueue the


neighbours of E. Also, add E as the ORIG of its neighbours. Note that E has
two neighbours C and F. Since C has already been added to the queue and it
is not in the Ready state, we will not add C and add only F.
Solution

6 - Dequeue a node by setting FRONT = FRONT + 1 and enqueue the


neighbours of G. Also, add G as the ORIG of its neighbours. Note that G
has three neighbours F, H, and I. Since F has already been added to the
queue, we will only add H and I.

As I is our final destination, we stop the execution of this algorithm as soon as it is encountered and added to the
QUEUE.

Now, backtrack from I using ORIG to find the minimum path P. Thus, we have P as A -> C -> G -> I.
Applications of Breadth-First Search Algorithm

 Breadth-first search can be used to solve many problems such as:

 Finding all connected components in a graph G.


 Finding all nodes within an individual connected component.
 Finding the shortest path between two nodes, u and v, of an unweighted graph.
 Finding the shortest path between two nodes, u and v, of a weighted graph.
13.6.2 Depth-first Search Algorithm

 The depth-first search algorithm progresses by expanding the


starting node of G and then going deeper and deeper until the
goal node is found, or until a node that has no children is
encountered.

 This algorithm is similar to the in-order traversal of a binary


tree.

 Its implementation is similar to that of the breadth-first search


algorithm but here we use a stack instead of a queue.

 Again, we use a variable STATUS to represent the current state of


the node.
Example 13.2

 Consider the graph G given and the adjacency list of G is also given as in the figure

 Suppose we want to print all the nodes that can be reached from the node H (including H itself).

 One alternative is to use a depth-first search of G starting at node H.


Example 13.2

A - Push H onto the stack.

B - Pop and print the top element of the STACK, that is, H. Push all
the neighbours of H onto the stack that are in the ready state. The
STACK now becomes

C - Pop and print the top element of the STACK, that is, I. Push all the
neighbours of I onto the stack that are in the ready state. The STACK now
becomes
Example 13.2

D - Pop and print the top element of the STACK, that is, F. Push all the
neighbours of F onto the stack that are in the ready state. (Note F has
two neighbours, C and H. But only C will be added, as H is not in the
ready state.) The STACK now becomes

E - Pop and print the top element of the STACK, that is, C. Push all the
neighbours of C onto the stack that are in the ready state. The STACK
now becomes
Example 13.2

F - Pop and print the top element of the STACK, that is, G. Push all the
neighbours of G onto the stack that are in the ready state. Since there
are no neighbours of G that are in the ready state,no push operation is
performed. The STACK now becomes

G - Pop and print the top element of the STACK, that is, B. Push all the
neighbours of B onto the stack that are in the ready state. Since there
are no neighbours of B that are in the ready state, no push operation is
performed. The STACK now becomes
Example 13.2

H - Pop and print the top element of the STACK, that is, E. Push all the
neighbours of E onto the stack that are in the ready state. Since there
are no neighbours of E that are in the ready state, no push operation is
performed. The STACK now becomes empty.

Since the STACK is now empty, the depth-first search of G starting at node H is complete and the nodes which were
printed are:

H, I, F, C, G, B, E

These are the nodes which are reachable from the node H.
Applications of Depth-First Search Algorithm

Depth-first search is useful for:


 Finding a path between two specified nodes, u and v, of an unweighted graph.
 Finding a path between two specified nodes, u and v, of a weighted graph.
 Finding whether a graph is connected or not.
 Computing the spanning tree of a connected graph.
Programming E
xample
13.8 SHORTEST PATH ALGORITHMS

1. Minimum spanning tree (adjacency list)


2. Dijkstra’s algorithm
3. Warshall’s algorithm

While the first two use an adjacency list to find the shortest path, Warshall’s algorithm uses an
adjacency matrix to do the same.
13.8.1 Minimum Spanning Trees

A spanning tree of a connected, undirected graph G is a sub-graph of G which is a tree that connects
all the vertices together.

A graph G can have many different spanning trees.


13.8.1 Minimum Spanning Trees

 We can assign weights to each edge (which is cost of the edge)


 We can calculate the sum of the weights of the edges in that spanning tree

 A minimum spanning tree (MST) is defined as a spanning tree with weight less than or equal to the weight of
every other spanning tree.

MST
Applications of Minimum Spanning Trees

1. MSTs are widely used for designing networks. A minimum spanning tree is used to determine the least
costly paths with no cycles in this network, thereby providing a connection that has the minimum cost
involved.

2. MSTs are used to find airline routes. While the vertices in the graph denote cities, edges represent the routes
between these cities. MSTs are used to optimize airline routes by finding the least costly path with no cycles.

3. MSTs are also used to find the cheapest way to connect terminals, such as cities, electronic components or
computers via roads, airlines, railways, wires or telephone lines.

4. MSTs are applied in routing algorithms for finding the most efficient path.
13.8.2 Prim’s Algorithm
Prim’s algorithm is aa algorithm that is used to form a minimum spanning tree for a connected weighted undirected graph.

For this, the algorithm maintains three sets of vertices which can be given as below:

Tree vertices Vertices that are a part of the minimum spanning tree T.
Fringe vertices Vertices that are currently not a part of T, but are adjacent to some tree vertex.
Unseen vertices Vertices that are neither tree vertices nor fringe vertices fall under thiscategory.
Example 13.7 Construct a minimum spanning tree of the graph given in Fig.
Example
13.8.3 Kruskal’s Algorithm

 Kruskal’s algorithm is used to find the minimum spanning tree for a connected weighted graph.
 The algorithm aims to find a subset of the edges that forms a tree that includes every vertex.
 The total weight of all the edges in the tree is minimized.
 However, if the graph is not connected, then it finds a minimum spanning forest.
 Note that a forest is a collection of trees.
 Similarly, a minimum spanning forest is a collection of minimum spanning trees.
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure

A
1

B C D

E F
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure

A
1

B C D

E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure

A
1

B C D
3
E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure

A
1

B C D
4
3
E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure

A
1

B C D
4
3
E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure

A
1

B C D
4
3
E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure

A
7 1
B C D
4
3
E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure

A
7 1
B C D
4
3
E F
2
13.8.4 Dijkstra’s Algorithm

Dijkstra’s algorithm, given by a Dutch scientist Edsger Dijkstra in 1959, is


used to find the shortest path tree.

This algorithm is widely used in network routing protocols, most notably IS-
IS and OSPF (Open Shortest Path First).

Given a graph G and a source node A, the algorithm is used to find the
shortest path (one having the lowest cost) between A (source node) and
every other node.

Moreover, Dijkstra’s algorithm is also used for finding the costs of the
shortest paths from a source node to a destination node. Edsger Wybe Dijkstra
1930 - 2002
Algorithm

Dijkstra’s algorithm is used to find the length of an optimal path between two nodes in a graph.
The term optimal can mean anything, shortest, cheapest, or fastest.
If we start the algorithm with an initial node, then the distance of a node Y can be given as the distance from the
initial node to that node.
Difference between Dijkstra’s Algorithm and Minimum Spanning Tree

Minimum spanning tree algorithm is used to traverse a graph in the most efficient manner
Dijkstra’s algorithm calculates the distance from a given vertex to every other vertex in the graph.

Dijkstra’s algorithm is very similar to Prim’s algorithm. Both the algorithms begin at a specific
node and extend outward within the graph, until all other nodes in the graph have been reached.

The point where these algorithms differ is that while Prim’s algorithm stores a minimum cost
edge, Dijkstra’s algorithm stores the total cost from a source node to the current node.

Moreover, Dijkstra’s algorithm is used to store the summation of minimum cost edges, while Prim’s algorithm
stores at most one minimum cost edge.
Next Lesson
Chapter14: Searching and Sorting

You might also like