Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data structures (Graph) (1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

GRAPHS

Instructor:
Ms.Dur-e-Shawar Agha
ROAD MAP
 Introduction to Graph

 Types of Graph

 Breadth First Search (BDS)

 Depth First Search (DFS)

 Dijkstra's algorithm

 Prims algorithm
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. Take a look at the following graph.

In the above graph,


V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
Applications of graph

In Computer science graphs are used to represent the flow of computation.

• Google maps uses graphs for building transportation systems

• In Facebook, users are considered to be the vertices and if they are friends then there
is an edge running between them

• In World Wide Web, web pages are considered to be the vertices.

• In Operating System, we come across the Resource Allocation Graph where each
process and resources are considered to be vertices.
Types of graphs

 Weighted Graph
 Unweighted Graph
 Undirected Graph
 Directed Graph
 Complete Graph
Weighted graphs

• Graphs whose edges or paths have values.

• All the values seen associated with the edges are called
weights.

• Edges value can represent weight/cost/length.


Unweighted graphs

• Where there is no value or weight associated with the edge.

• By default, all the graphs are unweighted unless there is a value associated.
Directed graph
• Directed graph also called a digraph, where a set of objects (V E) are connected

• All the edges are directed from one node to another.

• The below image showcases the directed graph.


Undirected graph

• Where a set of objects are connected, and all the edges are bidirectional.

• The below image showcases the undirected graph.


Complete graph

• A complete graph is a graph in which every vertex has an edge to


all other vertices is called a complete graph
• In other words, each pair of graph vertices is connected by an
edge.
• The number of edges in a complete graph can be calculated
through following formula:
Edged = [V(V - 1)] / 2
Graph Representation

The two most commonly used data structures to store graphs are:
1. Adjacency List
2. Adjacency Matrix
1. Adjacency List

• An adjacency list represents a graph as an array of linked lists.


• The index of the array represents a vertex and each element in its linked list represents
the other vertices that form an edge with the vertex.
• Here nodes are stored as an index of the one-dimension array followed by edges being
stored as a list.
2. Adjacency Matrix
• An adjacency matrix is a way of representing a graph G = {V, E} as a matrix of Booleans.
• The size of the matrix is VxV where V is the number of vertices in the graph and the
value of an entry Aij is either 1 or 0 depending on whether there is an edge from vertex i
to vertex j
• Here nodes are represented as the index of a two-dimensional array, followed by edges
represented as non-zero values of an adjacent matrix.
Graph Traversal
• Graph traversal is a technique used for a searching vertex in a graph.
• The graph traversal is also used to decide the order of vertices is visited in the search
process.
• A graph traversal finds the edges to be used in the search process without creating
loops.
• That means using graph traversal we visit all the vertices of the graph without getting
into looping path.
• There are two graph traversal techniques and they are as follows...
1. DFS (Depth First Search)
2. BFS (Breadth First Search)
1. Depth First Search (DFS)

• The DFS search begins starting from the first node and goes deeper and deeper,
exploring down until the targeted node is found.

• If the targeted key is not found, the search path is changed to the path that was
stopped exploring during the initial search, and the same procedure is repeated for that
branch.

• We use Stack data structure with maximum size of total number of vertices in the
graph to implement DFS traversal.
Contd….

We use the following steps to implement DFS traversal...


Step 1 - Define a Stack of size total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on
to the Stack.
Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top
of stack and push it on to the stack.
Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is
at the top of the stack.
Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex
from the stack.
Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
Step 7 - When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph
2. Breadth First Search (BFS)

• Breadth-First Search navigates a graph in a breadth motion and utilizes based on


the Queue to jump from one node to another, after encountering an end in the
path.

• We use Queue data structure with maximum size of total number of vertices in the
graph to implement BFS traversal.
Contd….

We use the following steps to implement BFS traversal...


Step 1 - Define a Queue of size total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the
Queue and insert them into the Queue.
Step 4 - When there is no new vertex to be visited from the vertex which is at front of
the Queue then delete that vertex.
Step 5 - Repeat steps 3 and 4 until queue becomes empty.
Step 6 - When queue becomes empty, then produce final spanning tree by removing
unused edges from the graph
Example

Consider the following example to perform BFS and DFS traversal


Start Node: “S”

A B C

D
BFS(Breadth First Search)
DFS(Breadth First Search)
Dijkstra’s Algorithm
 It is a solution to the single-source shortest path problem in graph theory.

 Works on both directed and undirected graphs. However, all edges must have
nonnegative weights.

 Approach: Greedy

 Input: Weighted graph G={E,V} and source vertex v∈V, such that all edge
weights are nonnegative

 Output: Lengths of shortest paths (or the shortest paths themselves) from a
given source vertex v∈V to all other vertices
Pseudocode

dist[s] ←0 (distance to source vertex is zero)


for all v ∈ V–{s}
do dist[v] ←∞ (set all other distances to infinity)
S←∅ (S, the set of visited vertices is initially empty)
Q←V (Q, the queue initially contains all vertices)
while Q ≠∅ (while the queue is not empty)
do u ← mindistance(Q,dist)(select the element of Q with the min. distance)
S←S∪{u} (add u to list of visited vertices)
for all v ∈ neighbors[u]
do if dist[v] > dist[u] + w(u, v) (if new shortest path found)
then d[v] ←d[u] + w(u, v) (set new value of shortest path)
(if desired, add traceback code)
return dist
Example
Implementations and Running Times

 The simplest implementation is to store vertices in an array or linked list. This


will produce a running time of

 O(|V|^2 + |E|)

 For sparse graphs, or graphs with very few edges and many nodes, it can be
implemented more efficiently storing the graph in an adjacency list using a
binary heap or priority queue. This will produce a running time of

 O((|E|+|V|) log |V|)


Applications
- Traffic Information Systems are most prominent use
- Mapping (Map Quest, Google Maps)
- Routing Systems
Prim’s Algorithm

 Prim’s Algorithm is a famous greedy algorithm.

 It is used for finding the Minimum Spanning Tree (MST) of a given graph.

 To apply Prim’s algorithm, the given graph must be weighted, connected and
undirected.
Prim’s Algorithm Implementation

Step-01:

 Randomly choose any vertex.

 The vertex connecting to the edge having least weight is usually selected.
Step-02:

 Find all the edges that connect the tree to new vertices.

 Find the least weight edge among those edges and include it in the existing tree.

 If including that edge creates a cycle, then reject that edge and look for the next
least weight edge.
Step-03:

 Keep repeating step-02 until all the vertices are included and Minimum
Spanning Tree (MST) is obtained.
PRACTICE PROBLEMS BASED ON PRIM’S ALGORITHM

Construct the minimum spanning tree (MST) for the given graph using
Prim’s Algorithm.
The above discussed steps are followed to find the minimum cost spanning
tree using Prim’s Algorithm
Step-01:
Since all the vertices have been included in the MST, so we stop.

Now, Cost of Minimum Spanning Tree

= Sum of all edge weights

= 10 + 25 + 22 + 12 + 16 + 14

= 99 units
Activity#01

Consider the following example to perform BFS and DFS traversal


Start Node: “A”
Activity#02

Consider the following example to perform Dijisktra’s Algorithm


Start Node: “1”
Activity#03

Consider the following example to perform Prim’s Algorithm


Start Node: “1”
SUMMARY
 Introduction to Graph

 Types of Graph

 Graph Traversals

You might also like