SlideShare a Scribd company logo
Graph
Prepared By : Ketaki Pattani
Enroll. No. : 130210107039
1
4
76
3 5
2
1 2
3
Some related terms:
• Graph: A graph G is a pair (V,E), where V is a finite
set of points called vertices and E is a finite set of
edges.
• Path: A path from a vertex v to a vertex u is a
sequence <v0,v1,v2,…,vk> of vertices where v0 ,v1,
v2… are the set of vertices followed to move from a
source to destination.
• Pathlength:The length of a path is defined as the
number of edges in the path.
Related terms: Degree of vertex
• Degree of vertex: It represents
the total number of edges linked
to a particular vertex.
• Indegree: It is the number of
incomming edges to a vertex.
•Outdegree: It is the number of
outgoing edges from a vertex.
•Source: A vertex having indegree
zero is a source.
•Sink: A vertex having outdegree
zero is a sink.
Some related terms:
• Undirected Graph: An
undirected graph is the
graph consisting of
unordered pair of vertices .
For eg :figure (a)
• Directed Graph : In a
directed graph, the edge e is
an ordered pair (u,v) which
is incident from vertex u and
is incident to vertex v. For
eg : fig(b)
Some related terms: Weighted graph
• A weighted graph is a graph
where each edge is assigned
a particular value.
• This may be the distance or
the cost.
• Here, the graph G(V,E) may
be directed or undirected
graph.
• Also the weight may be
represented by ‘W’.
Graph Representation:
Adjacency Matrix:
• Here, a two dimensional
square matrix can be used
to store a graph.
• Adjacency matrix for
undirected graph is always
symmetric .
Adjacency List
• Adjacency List creates a
seperste linked list for each
adjacent vertex.
• Adjacency graph for n nodes
is represented by an Array
of Pointers.
a
dc
b a
b
c
d
b
a
d
d c
c
a b
a c
a
dc
b
1 2
3 4
1 2 3 4
1 0 1 1 1
2 1 0 1 0
3 1 1 0 1
4 1 0 1 0
Graph Traversal:
• Most of the applications of graphs require the
traversal of graphs.
• Traversal means visiting each node in a graph
exactly once.
• It also stands for that there must be no cycles in
the traversal.
• The two commonly used techniques are:
(1) Depth First Search(DFS)
(2) Breadth First Search(BFS)
Breadth First Search:
• Strategy
 choose a starting vertex,
distance d = 0
 vertices are visited in order
of increasing distance from
the starting vertex,
 examine all edges leading
from vertices (at distance
d) to adjacent vertices (at
distance d+1)
 then, examine all edges
leading from vertices at
distance d+1 to distance
d+2, and so on,
 until no new vertex is
discovered
Algorithm for BFS:
1. for each vertex u in V[G] – {s}
2 do color[u]  white
3 d[u]  
4 [u]  nil
5 color[s]  gray
6 d[s]  0
7 [s]  nil
8 Q  
9 enqueue(Q,s)
10 while Q  
11 do u  dequeue(Q)
12 for each v in Adj[u]
13 do if color[v] = white
14 then color[v]  gray
15 d[v]  d[u] + 1
16 [v]  u
17 enqueue(Q,v)
18 color[u]  black
Example (BFS)
 0
  
 

r s t u
v w x y
Q: s
0
(Courtesy of Prof. Jim Anderson)
Example (BFS)
1 0
1  
 

r s t u
v w x y
Q: w r
1 1
Example (BFS)
1 0
1 2 
2 

r s t u
v w x y
Q: r t x
1 2 2
Example (BFS)
1 0
1 2 
2 
2
r s t u
v w x y
Q: t x v
2 2 2
Example (BFS)
1 0
1 2 
2 3
2
r s t u
v w x y
Q: x v u
2 2 3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q: v u y
2 3 3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q: u y
3 3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q: y
3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q: 
Use of a queue in BFS Graph:
• It is very common to use a queue to keep
track of:
– nodes to be visited next, or
– nodes that we have already visited.
• Typically, use of a queue leads to a breadth-
first visit order.
• Breadth-first visit order is “cautious” in the
sense that it examines every path of length i
before going on to paths of length i+1.
Depth First Search:
• Strategy
– choose a starting vertex,
distance d = 0
– examine One edges leading
from vertices (at distance d)
to adjacent vertices (at
distance d+1)
– then, examine One edges
leading from vertices at
distance d+1 to distance d+2,
and so on,
– until no new vertex is
discovered, or dead end
– then, backtrack one distance
back up, and try other edges,
and so on.
Algorithm for DFS:
.
Example (DFS)
1/
u v w
x y z
Example (DFS)
1/ 2/
u v w
x y z
Example (DFS)
1/
3/
2/
u v w
x y z
Example (DFS)
1/
4/ 3/
2/
u v w
x y z
Example (DFS)
1/
4/ 3/
2/
u v w
x y z
B
Example (DFS)
1/
4/5 3/
2/
u v w
x y z
B
Example (DFS)
1/
4/5 3/6
2/
u v w
x y z
B
Example (DFS)
1/
4/5 3/6
2/7
u v w
x y z
B
Example (DFS)
1/
4/5 3/6
2/7
u v w
x y z
BF
Example (DFS)
1/8
4/5 3/6
2/7
u v w
x y z
BF
Example (DFS)
1/8
4/5 3/6
2/7 9/
u v w
x y z
BF
Example (DFS)
1/8
4/5 3/6
2/7 9/
u v w
x y z
BF C
Example (DFS)
1/8
4/5 3/6 10/
2/7 9/
u v w
x y z
BF C
Example (DFS)
1/8
4/5 3/6 10/
2/7 9/
u v w
x y z
BF C
B
Example (DFS)
1/8
4/5 3/6 10/11
2/7 9/
u v w
x y z
BF C
B
Example (DFS)
1/8
4/5 3/6 10/11
2/7 9/12
u v w
x y z
BF C
B
Use of a stack in DFS Graph
• It is very common to use a stack to keep track
of:
– nodes to be visited next, or
– nodes that we have already visited.
• Typically, use of a stack leads to a depth-first
visit order.
• Depth-first visit order is “aggressive” in the
sense that it examines complete paths.
Minimum Spanning Tree (MST)
• it is a tree (i.e., it is acyclic)
• it covers all the vertices V
– contains |V| - 1 edges
• the total cost associated with tree edges is the
minimum among all possible spanning trees
A minimum spanning tree is a subgraph of an
undirected weighted graph G as shown, such that
Minimum Spanning Tree: Prim's
Algorithm
• Prim's algorithm for finding an MST is a greedy
algorithm.
• Start by selecting an arbitrary vertex, include
it into the current MST.
• Grow the current MST by inserting into it the
vertex closest to one of the vertices already in
current MST.
Minimum Spanning Tree: Prim's Algorithm
.
Minimum Spanning Tree: Prim's
Algorithm
.
Dijkstra's algorithm
• For a weighted graph G = (V,E,w), the single-
source shortest paths problem is to find the
shortest paths from a vertex v ∈ V to all other
vertices in V.
• Dijkstra's algorithm is similar to Prim's algorithm.
– maintains a set of nodes for which the shortest paths
are known.
– grow this set based on the node closest to source
using one of the nodes in the current shortest path
set.
Single-Source Shortest Paths: Dijkstra's
Algorithm
.
Floyd's Algorithm
• For any pair of vertices vi, vj ∈ V, consider all paths
from vi to vj whose intermediate vertices belong to
the set {v1,v2,…,vk}.
• Let pi
(
,
k
j
) (of weight di
(
,
k
j
)) be the minimum-weight
path among them.
• If vertex vk is not in the shortest path from vi to vj,
then pi
(
,
k
j
) is the same as pi
(
,
k
j
-1).
• If f vk is in pi
(
,
k
j
), then we can break pi
(
,
k
j
) into two
paths - one from vi to vk and one from vk to vj . Each
of these paths uses vertices from {v1,v2,…,vk-1}.
Floyd's Algorithm
From our observations, the following recurrence
relation follows:
This equation must be computed for each pair of nodes and
for k = 1, n. The serial complexity is O(n3).
Floyd's Algorithm
Floyd's all-pairs shortest paths algorithm.
This program computes the all-pairs shortest paths
of the graph G = (V,E) with adjacency matrix A.
130210107039 2130702

More Related Content

PPT
Breadth first search and depth first search
PPTX
Bfs & dfs application
PPT
Bfs and dfs in data structure
PPT
Graphs bfs dfs
PPT
PPT
Depth First Search ( DFS )
PDF
PPTX
Breadth first search and depth first search
Bfs & dfs application
Bfs and dfs in data structure
Graphs bfs dfs
Depth First Search ( DFS )

What's hot (20)

PDF
Bfs dfs
PPT
Breadth first search
PPT
Depth firstsearchalgorithm
PPTX
Graph Algorithms: Breadth-First Search (BFS)
PPTX
Presentation on Breadth First Search (BFS)
PPTX
Depth-First Search
PPTX
Breadth first search (Bfs)
PPT
2.5 bfs & dfs 02
PPTX
Bfs and Dfs
PPT
lecture 17
PPT
Graph traversal-BFS & DFS
PPTX
Breadth First Search (BFS)
PDF
Skiena algorithm 2007 lecture11 breadth deapth first search
PPTX
Depth first search [dfs]
PDF
Depth First Search and Breadth First Search
PPT
2.5 graph dfs
PPTX
DFS and BFS
PPT
lecture 18
PPT
Bfs
PPTX
Data structures and algorithms lab7
Bfs dfs
Breadth first search
Depth firstsearchalgorithm
Graph Algorithms: Breadth-First Search (BFS)
Presentation on Breadth First Search (BFS)
Depth-First Search
Breadth first search (Bfs)
2.5 bfs & dfs 02
Bfs and Dfs
lecture 17
Graph traversal-BFS & DFS
Breadth First Search (BFS)
Skiena algorithm 2007 lecture11 breadth deapth first search
Depth first search [dfs]
Depth First Search and Breadth First Search
2.5 graph dfs
DFS and BFS
lecture 18
Bfs
Data structures and algorithms lab7
Ad

Viewers also liked (12)

PPT
Application of dfs
PPTX
Search algorithms master
PPTX
Linear and Binary Search Algorithms.(Discrete Mathematics)
PPT
Ch2 3-informed (heuristic) search
PPT
Heuristic Search
PDF
ADA complete notes
PDF
Algorithm Analysis and Design Class Notes
ODP
Hillclimbing search algorthim #introduction
PPTX
Design and Analysis of Algorithms
PPT
Hill climbing
PPT
Heuristic Search Techniques {Artificial Intelligence}
PPT
17. Trees and Graphs
Application of dfs
Search algorithms master
Linear and Binary Search Algorithms.(Discrete Mathematics)
Ch2 3-informed (heuristic) search
Heuristic Search
ADA complete notes
Algorithm Analysis and Design Class Notes
Hillclimbing search algorthim #introduction
Design and Analysis of Algorithms
Hill climbing
Heuristic Search Techniques {Artificial Intelligence}
17. Trees and Graphs
Ad

Similar to 130210107039 2130702 (20)

PPT
Data Structures-Non Linear DataStructures-Graphs
PPTX
DATA STRUCTURES.pptx
PPTX
logic.pptx
PPT
14. GRAPH in data structures and algorithm.ppt
PDF
Unit-10 Graphs .pdf
PPTX
Algorithms and data Chapter 3 V Graph.pptx
PPTX
Basic Graph Algorithms Vertex (Node): lk
PPT
Chapter 23 aoa
PPT
Unit VI - Graphs.ppt
PPTX
Graph Data Structure
PPTX
Unit ix graph
PPT
Graphs
PPTX
UNIT III.pptx
PPTX
Unit 9 graph
PPT
Graphs Presentation of University by Coordinator
PPTX
Graph data structures for ppt for understanding.pptx
PPSX
Design and analysis of Algorithms Lecture 1 (BFS, DFS).ppsx
PDF
Daa chpater 12
PPTX
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures-Non Linear DataStructures-Graphs
DATA STRUCTURES.pptx
logic.pptx
14. GRAPH in data structures and algorithm.ppt
Unit-10 Graphs .pdf
Algorithms and data Chapter 3 V Graph.pptx
Basic Graph Algorithms Vertex (Node): lk
Chapter 23 aoa
Unit VI - Graphs.ppt
Graph Data Structure
Unit ix graph
Graphs
UNIT III.pptx
Unit 9 graph
Graphs Presentation of University by Coordinator
Graph data structures for ppt for understanding.pptx
Design and analysis of Algorithms Lecture 1 (BFS, DFS).ppsx
Daa chpater 12
Data Structures and Agorithm: DS 21 Graph Theory.pptx

Recently uploaded (20)

PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Trump Administration's workforce development strategy
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PPTX
Introduction to Building Materials
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
RMMM.pdf make it easy to upload and study
PDF
Empowerment Technology for Senior High School Guide
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Weekly quiz Compilation Jan -July 25.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Trump Administration's workforce development strategy
LDMMIA Reiki Yoga Finals Review Spring Summer
Chinmaya Tiranga quiz Grand Finale.pdf
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Introduction to Building Materials
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Final Presentation General Medicine 03-08-2024.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Unit 4 Skeletal System.ppt.pptxopresentatiom
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Final Presentation General Medicine 03-08-2024.pptx
Computing-Curriculum for Schools in Ghana
RMMM.pdf make it easy to upload and study
Empowerment Technology for Senior High School Guide
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf

130210107039 2130702

  • 1. Graph Prepared By : Ketaki Pattani Enroll. No. : 130210107039 1 4 76 3 5 2 1 2 3
  • 2. Some related terms: • Graph: A graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite set of edges. • Path: A path from a vertex v to a vertex u is a sequence <v0,v1,v2,…,vk> of vertices where v0 ,v1, v2… are the set of vertices followed to move from a source to destination. • Pathlength:The length of a path is defined as the number of edges in the path.
  • 3. Related terms: Degree of vertex • Degree of vertex: It represents the total number of edges linked to a particular vertex. • Indegree: It is the number of incomming edges to a vertex. •Outdegree: It is the number of outgoing edges from a vertex. •Source: A vertex having indegree zero is a source. •Sink: A vertex having outdegree zero is a sink.
  • 4. Some related terms: • Undirected Graph: An undirected graph is the graph consisting of unordered pair of vertices . For eg :figure (a) • Directed Graph : In a directed graph, the edge e is an ordered pair (u,v) which is incident from vertex u and is incident to vertex v. For eg : fig(b)
  • 5. Some related terms: Weighted graph • A weighted graph is a graph where each edge is assigned a particular value. • This may be the distance or the cost. • Here, the graph G(V,E) may be directed or undirected graph. • Also the weight may be represented by ‘W’.
  • 6. Graph Representation: Adjacency Matrix: • Here, a two dimensional square matrix can be used to store a graph. • Adjacency matrix for undirected graph is always symmetric . Adjacency List • Adjacency List creates a seperste linked list for each adjacent vertex. • Adjacency graph for n nodes is represented by an Array of Pointers. a dc b a b c d b a d d c c a b a c a dc b 1 2 3 4 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0
  • 7. Graph Traversal: • Most of the applications of graphs require the traversal of graphs. • Traversal means visiting each node in a graph exactly once. • It also stands for that there must be no cycles in the traversal. • The two commonly used techniques are: (1) Depth First Search(DFS) (2) Breadth First Search(BFS)
  • 8. Breadth First Search: • Strategy  choose a starting vertex, distance d = 0  vertices are visited in order of increasing distance from the starting vertex,  examine all edges leading from vertices (at distance d) to adjacent vertices (at distance d+1)  then, examine all edges leading from vertices at distance d+1 to distance d+2, and so on,  until no new vertex is discovered
  • 9. Algorithm for BFS: 1. for each vertex u in V[G] – {s} 2 do color[u]  white 3 d[u]   4 [u]  nil 5 color[s]  gray 6 d[s]  0 7 [s]  nil 8 Q   9 enqueue(Q,s) 10 while Q   11 do u  dequeue(Q) 12 for each v in Adj[u] 13 do if color[v] = white 14 then color[v]  gray 15 d[v]  d[u] + 1 16 [v]  u 17 enqueue(Q,v) 18 color[u]  black
  • 10. Example (BFS)  0       r s t u v w x y Q: s 0 (Courtesy of Prof. Jim Anderson)
  • 11. Example (BFS) 1 0 1      r s t u v w x y Q: w r 1 1
  • 12. Example (BFS) 1 0 1 2  2   r s t u v w x y Q: r t x 1 2 2
  • 13. Example (BFS) 1 0 1 2  2  2 r s t u v w x y Q: t x v 2 2 2
  • 14. Example (BFS) 1 0 1 2  2 3 2 r s t u v w x y Q: x v u 2 2 3
  • 15. Example (BFS) 1 0 1 2 3 2 3 2 r s t u v w x y Q: v u y 2 3 3
  • 16. Example (BFS) 1 0 1 2 3 2 3 2 r s t u v w x y Q: u y 3 3
  • 17. Example (BFS) 1 0 1 2 3 2 3 2 r s t u v w x y Q: y 3
  • 18. Example (BFS) 1 0 1 2 3 2 3 2 r s t u v w x y Q: 
  • 19. Use of a queue in BFS Graph: • It is very common to use a queue to keep track of: – nodes to be visited next, or – nodes that we have already visited. • Typically, use of a queue leads to a breadth- first visit order. • Breadth-first visit order is “cautious” in the sense that it examines every path of length i before going on to paths of length i+1.
  • 20. Depth First Search: • Strategy – choose a starting vertex, distance d = 0 – examine One edges leading from vertices (at distance d) to adjacent vertices (at distance d+1) – then, examine One edges leading from vertices at distance d+1 to distance d+2, and so on, – until no new vertex is discovered, or dead end – then, backtrack one distance back up, and try other edges, and so on.
  • 32. Example (DFS) 1/8 4/5 3/6 2/7 9/ u v w x y z BF
  • 33. Example (DFS) 1/8 4/5 3/6 2/7 9/ u v w x y z BF C
  • 34. Example (DFS) 1/8 4/5 3/6 10/ 2/7 9/ u v w x y z BF C
  • 35. Example (DFS) 1/8 4/5 3/6 10/ 2/7 9/ u v w x y z BF C B
  • 36. Example (DFS) 1/8 4/5 3/6 10/11 2/7 9/ u v w x y z BF C B
  • 37. Example (DFS) 1/8 4/5 3/6 10/11 2/7 9/12 u v w x y z BF C B
  • 38. Use of a stack in DFS Graph • It is very common to use a stack to keep track of: – nodes to be visited next, or – nodes that we have already visited. • Typically, use of a stack leads to a depth-first visit order. • Depth-first visit order is “aggressive” in the sense that it examines complete paths.
  • 39. Minimum Spanning Tree (MST) • it is a tree (i.e., it is acyclic) • it covers all the vertices V – contains |V| - 1 edges • the total cost associated with tree edges is the minimum among all possible spanning trees A minimum spanning tree is a subgraph of an undirected weighted graph G as shown, such that
  • 40. Minimum Spanning Tree: Prim's Algorithm • Prim's algorithm for finding an MST is a greedy algorithm. • Start by selecting an arbitrary vertex, include it into the current MST. • Grow the current MST by inserting into it the vertex closest to one of the vertices already in current MST.
  • 41. Minimum Spanning Tree: Prim's Algorithm .
  • 42. Minimum Spanning Tree: Prim's Algorithm .
  • 43. Dijkstra's algorithm • For a weighted graph G = (V,E,w), the single- source shortest paths problem is to find the shortest paths from a vertex v ∈ V to all other vertices in V. • Dijkstra's algorithm is similar to Prim's algorithm. – maintains a set of nodes for which the shortest paths are known. – grow this set based on the node closest to source using one of the nodes in the current shortest path set.
  • 44. Single-Source Shortest Paths: Dijkstra's Algorithm .
  • 45. Floyd's Algorithm • For any pair of vertices vi, vj ∈ V, consider all paths from vi to vj whose intermediate vertices belong to the set {v1,v2,…,vk}. • Let pi ( , k j ) (of weight di ( , k j )) be the minimum-weight path among them. • If vertex vk is not in the shortest path from vi to vj, then pi ( , k j ) is the same as pi ( , k j -1). • If f vk is in pi ( , k j ), then we can break pi ( , k j ) into two paths - one from vi to vk and one from vk to vj . Each of these paths uses vertices from {v1,v2,…,vk-1}.
  • 46. Floyd's Algorithm From our observations, the following recurrence relation follows: This equation must be computed for each pair of nodes and for k = 1, n. The serial complexity is O(n3).
  • 47. Floyd's Algorithm Floyd's all-pairs shortest paths algorithm. This program computes the all-pairs shortest paths of the graph G = (V,E) with adjacency matrix A.