SlideShare ist ein Scribd-Unternehmen logo
1 von 52
Prof. Sandeep Vishwakarma
Chandrabhan sharma college Powai
FYBCA Sem-II
• Introduction
Terminologies: graph, node (Vertices), arcs (edge), directed graph, in -
degree, out -degree, adjacent, successor, predecessor, relation,
weight, path, length.
• Representations of a graph
a. Array Representation
b. Linked list Representation
• Traversal of graphs
a. Depth-first search (DFS).
b. Breadth-first search (BFS).
• Applications of Graph
Data structure using C ++ (CMP505)
Unit-7
Graph
Data Structure and Algorithm
Data Structure and Algorithm
Data Structure and Algorithm
Data Structure and Algorithm
Data Structure and Algorithm
Graph data structure is represented using
following representations...
 Adjacency Matrix
 Incidence Matrix
 Adjacency List
Data Structure and Algorithm
 In this representation, the graph is represented
using a matrix of size total number of vertices
by a total number of vertices. That means a
graph with 4 vertices is represented using a
matrix of size 4X4. In this matrix, both rows
and columns represent vertices. This matrix is
filled with either 1 or 0. Here, 1 represents that
there is an edge from row vertex to column
vertex and 0 represents that there is no edge
from row vertex to column vertex.
Data Structure and Algorithm
Data Structure and Algorithm
Directed Graph Representation
Undirected Graph Representation
 In this representation, the graph is represented using a
matrix of size total number of vertices by a total
number of edges. That means graph with 4 vertices and
6 edges is represented using a matrix of size 4X6. In
this matrix, rows represent vertices and columns
represents edges. This matrix is filled with 0 or 1 or -1.
Here, 0 represents that the row edge is not connected to
column vertex, 1 represents that the row edge is
connected as the outgoing edge to column vertex and -1
represents that the row edge is connected as the
incoming edge to column vertex.
Example of Incidence Matrix Representation
Adjacency List
Adjacency List using Array
Adjacency List using Link List
 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...
 DFS (Depth First Search)
 BFS (Breadth First Search)
Data Structure and Algorithm
 The basic idea behind this algorithm is that it
traverses the graph using recursion
◦ Go as far as possible until you reach a deadend
◦ Backtrack to the previous path and try the next
branch
◦ The graph below, started at node a, would be
visited in the following order: a, b, c, g, h, i, e, d,
f, j
d
a b
h i
c
g
e
j
f
 Vertices initially colored white
 Then colored gray when
discovered
 Then black when finished
Data Structure and Algorithm
 Discover time d[u]: when u is
first discovered
 Finish time f[u]: when backtrack
from u
 d[u] < f[u]
Data Structure and Algorithm
Data Structure and Algorithm
source
vertex
Data Structure and Algorithm
1 | | |
|
|
|
| |
source
vertex
d f
Data Structure and Algorithm
1 | | |
|
|
|
2 | |
source
vertex
d f
Data Structure and Algorithm
1 | | |
|
|
3 |
2 | |
source
vertex
d f
Data Structure and Algorithm
1 | | |
|
|
3 | 4
2 | |
source
vertex
d f
Data Structure and Algorithm
1 | | |
|
5 |
3 | 4
2 | |
source
vertex
d f
Data Structure and Algorithm
1 | | |
|
5 | 6
3 | 4
2 | |
source
vertex
d f
Data Structure and Algorithm
1 | | |
|
5 | 6
3 | 4
2 | 7 |
source
vertex
d f
Data Structure and Algorithm
1 | 8 | |
|
5 | 6
3 | 4
2 | 7 |
source
vertex
d f
Data Structure and Algorithm
1 | 8 | |
|
5 | 6
3 | 4
2 | 7 9 |
source
vertex
d f
Data Structure and Algorithm
1 | 8 | |
|
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
1 | 8 |11 |
|
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
1 |12 8 |11 |
|
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
1 |12 8 |11 13|
|
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
1 |12 8 |11 13|
14|
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
1 |12 8 |11 13|
14|15
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
1 |12 8 |11 13|16
14|15
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
Data Structure and Algorithm
DFS(G)
 for each vertex u in V,
 color[u]=white; p[u]=NIL
 time=0;
 for each vertex u in V
 if (color[u]=white)
 DFS-VISIT(u)
Data Structure and Algorithm
DFS-VISIT(u)
 color[u]=gray;
 time = time + 1;
 d[u] = time;
 for each v in Adj(u) do
 if (color[v] = white)
 p[v] = u;
 DFS-VISIT(v);
 color[u] = black;
 time = time + 1; f[u]= time;
DFS: Algorithm (Cont.)
source
vertex
BFS (Breadth First Search)
BFS Algorithms and Traversal
 Search for all vertices that are directly
reachable from the root (called level 1
vertices)
 After mark all these vertices, visit all vertices
that are directly reachable from any level 1
vertices (called level 2 vertices), and so on.
 In general, level k vertices are directly
reachable from a level k – 1 vertices
Data Structure and Algorithm
 White vertices have not been discovered
◦ All vertices start out white
 Grey vertices are discovered but not fully explored
◦ They may be adjacent to white vertices
 Black vertices are discovered and fully explored
◦ They are adjacent only to black and gray vertices
 Explore vertices by scanning adjacency list of grey
vertices
Data Structure and Algorithm
BFS(G, s)
 For each vertex u in V – {s},
 color[u] = white;
 d[u] = infinity;
 p[u] = NIL
 color[s] = GRAY; d[s] = 0; p[s] = NIL; Q = empty queue
 ENQUEUE(Q,s)
 while (Q not empty)
 u = DEQUEUE(Q)
 for each v  Adj[u]
 if color[v] = WHITE
 then color[v] = GREY
 d[v] = d[u] + 1; p[v] = u
 ENQUEUE(Q, v);
 color[u] = BLACK;

Data Structure and Algorithm








r s t u
v w x y
Data Structure and Algorithm


0





r s t u
v w x y
s
Q:
Data Structure and Algorithm
1

0
1




r s t u
v w x y
w
Q: r
Data Structure and Algorithm
1

0
1
2
2


r s t u
v w x y
r
Q: t x
Data Structure and Algorithm
1
2
0
1
2
2


r s t u
v w x y
Q: t x v
Data Structure and Algorithm
1
2
0
1
2
2
3

r s t u
v w x y
Q: x v u
Data Structure and Algorithm
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: v u y
Data Structure and Algorithm
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: u y
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: y
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: Ø
In computer science graph theory is used for
the study of algorithms like:
1. Dijkstra's Algorithm
2. Prims's Algorithm
3. Kruskal's Algorithm
Note: Above algorithms are used to find
shortest path between two vertices.
YCMOU_FYBCA_DS_Unit-7.ppt

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Heap_Sort1.pptx
Heap_Sort1.pptxHeap_Sort1.pptx
Heap_Sort1.pptx
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Linked list
Linked listLinked list
Linked list
 
Linked lists
Linked listsLinked lists
Linked lists
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 
Singly link list
Singly link listSingly link list
Singly link list
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
Functions & Recursion
Functions & RecursionFunctions & Recursion
Functions & Recursion
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
Dijkstra Searching Algorithms.pptx
Dijkstra Searching Algorithms.pptxDijkstra Searching Algorithms.pptx
Dijkstra Searching Algorithms.pptx
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
linked list
linked list linked list
linked list
 

Ähnlich wie YCMOU_FYBCA_DS_Unit-7.ppt

Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm EData Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
jeniihykdevara
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docx
randyburney60861
 
lecture 17
lecture 17lecture 17
lecture 17
sajinsc
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
Traian Rebedea
 

Ähnlich wie YCMOU_FYBCA_DS_Unit-7.ppt (20)

LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Graphs
GraphsGraphs
Graphs
 
Lecture13
Lecture13Lecture13
Lecture13
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdf
 
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm EData Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docx
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 
LAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxLAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptx
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
lecture 17
lecture 17lecture 17
lecture 17
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
 
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam LermaGraph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
 

Mehr von sandeep54552

Mehr von sandeep54552 (20)

Dijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxDijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptx
 
E_R-Diagram (2).pptx
E_R-Diagram (2).pptxE_R-Diagram (2).pptx
E_R-Diagram (2).pptx
 
Agents_AI.ppt
Agents_AI.pptAgents_AI.ppt
Agents_AI.ppt
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
Inheritance in c++
Inheritance in c++ Inheritance in c++
Inheritance in c++
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
 
Jsp tag library
Jsp tag libraryJsp tag library
Jsp tag library
 
Hill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligenceHill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligence
 
Session bean
Session beanSession bean
Session bean
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
A star algorithms
A star algorithmsA star algorithms
A star algorithms
 
Bfs new
Bfs newBfs new
Bfs new
 
Bfs new
Bfs newBfs new
Bfs new
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jsp
 
Enterprise java unit-2_chapter-3
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3
 
Enterprise java unit-2_chapter-2
Enterprise  java unit-2_chapter-2Enterprise  java unit-2_chapter-2
Enterprise java unit-2_chapter-2
 

Kürzlich hochgeladen

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Kürzlich hochgeladen (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 

YCMOU_FYBCA_DS_Unit-7.ppt

  • 1. Prof. Sandeep Vishwakarma Chandrabhan sharma college Powai FYBCA Sem-II • Introduction Terminologies: graph, node (Vertices), arcs (edge), directed graph, in - degree, out -degree, adjacent, successor, predecessor, relation, weight, path, length. • Representations of a graph a. Array Representation b. Linked list Representation • Traversal of graphs a. Depth-first search (DFS). b. Breadth-first search (BFS). • Applications of Graph Data structure using C ++ (CMP505) Unit-7 Graph
  • 2.
  • 3. Data Structure and Algorithm
  • 4. Data Structure and Algorithm
  • 5. Data Structure and Algorithm
  • 6. Data Structure and Algorithm
  • 7. Data Structure and Algorithm
  • 8. Graph data structure is represented using following representations...  Adjacency Matrix  Incidence Matrix  Adjacency List Data Structure and Algorithm
  • 9.  In this representation, the graph is represented using a matrix of size total number of vertices by a total number of vertices. That means a graph with 4 vertices is represented using a matrix of size 4X4. In this matrix, both rows and columns represent vertices. This matrix is filled with either 1 or 0. Here, 1 represents that there is an edge from row vertex to column vertex and 0 represents that there is no edge from row vertex to column vertex. Data Structure and Algorithm
  • 10. Data Structure and Algorithm Directed Graph Representation Undirected Graph Representation
  • 11.  In this representation, the graph is represented using a matrix of size total number of vertices by a total number of edges. That means graph with 4 vertices and 6 edges is represented using a matrix of size 4X6. In this matrix, rows represent vertices and columns represents edges. This matrix is filled with 0 or 1 or -1. Here, 0 represents that the row edge is not connected to column vertex, 1 represents that the row edge is connected as the outgoing edge to column vertex and -1 represents that the row edge is connected as the incoming edge to column vertex.
  • 12. Example of Incidence Matrix Representation
  • 13. Adjacency List Adjacency List using Array Adjacency List using Link List
  • 14.  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...  DFS (Depth First Search)  BFS (Breadth First Search)
  • 15. Data Structure and Algorithm  The basic idea behind this algorithm is that it traverses the graph using recursion ◦ Go as far as possible until you reach a deadend ◦ Backtrack to the previous path and try the next branch ◦ The graph below, started at node a, would be visited in the following order: a, b, c, g, h, i, e, d, f, j d a b h i c g e j f
  • 16.  Vertices initially colored white  Then colored gray when discovered  Then black when finished Data Structure and Algorithm
  • 17.  Discover time d[u]: when u is first discovered  Finish time f[u]: when backtrack from u  d[u] < f[u] Data Structure and Algorithm
  • 18. Data Structure and Algorithm source vertex
  • 19. Data Structure and Algorithm 1 | | | | | | | | source vertex d f
  • 20. Data Structure and Algorithm 1 | | | | | | 2 | | source vertex d f
  • 21. Data Structure and Algorithm 1 | | | | | 3 | 2 | | source vertex d f
  • 22. Data Structure and Algorithm 1 | | | | | 3 | 4 2 | | source vertex d f
  • 23. Data Structure and Algorithm 1 | | | | 5 | 3 | 4 2 | | source vertex d f
  • 24. Data Structure and Algorithm 1 | | | | 5 | 6 3 | 4 2 | | source vertex d f
  • 25. Data Structure and Algorithm 1 | | | | 5 | 6 3 | 4 2 | 7 | source vertex d f
  • 26. Data Structure and Algorithm 1 | 8 | | | 5 | 6 3 | 4 2 | 7 | source vertex d f
  • 27. Data Structure and Algorithm 1 | 8 | | | 5 | 6 3 | 4 2 | 7 9 | source vertex d f
  • 28. Data Structure and Algorithm 1 | 8 | | | 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f
  • 29. Data Structure and Algorithm 1 | 8 |11 | | 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f
  • 30. Data Structure and Algorithm 1 |12 8 |11 | | 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f
  • 31. Data Structure and Algorithm 1 |12 8 |11 13| | 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f
  • 32. Data Structure and Algorithm 1 |12 8 |11 13| 14| 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f
  • 33. Data Structure and Algorithm 1 |12 8 |11 13| 14|15 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f
  • 34. Data Structure and Algorithm 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f
  • 35. Data Structure and Algorithm DFS(G)  for each vertex u in V,  color[u]=white; p[u]=NIL  time=0;  for each vertex u in V  if (color[u]=white)  DFS-VISIT(u)
  • 36. Data Structure and Algorithm DFS-VISIT(u)  color[u]=gray;  time = time + 1;  d[u] = time;  for each v in Adj(u) do  if (color[v] = white)  p[v] = u;  DFS-VISIT(v);  color[u] = black;  time = time + 1; f[u]= time; DFS: Algorithm (Cont.) source vertex
  • 37. BFS (Breadth First Search) BFS Algorithms and Traversal
  • 38.  Search for all vertices that are directly reachable from the root (called level 1 vertices)  After mark all these vertices, visit all vertices that are directly reachable from any level 1 vertices (called level 2 vertices), and so on.  In general, level k vertices are directly reachable from a level k – 1 vertices Data Structure and Algorithm
  • 39.  White vertices have not been discovered ◦ All vertices start out white  Grey vertices are discovered but not fully explored ◦ They may be adjacent to white vertices  Black vertices are discovered and fully explored ◦ They are adjacent only to black and gray vertices  Explore vertices by scanning adjacency list of grey vertices Data Structure and Algorithm
  • 40. BFS(G, s)  For each vertex u in V – {s},  color[u] = white;  d[u] = infinity;  p[u] = NIL  color[s] = GRAY; d[s] = 0; p[s] = NIL; Q = empty queue  ENQUEUE(Q,s)  while (Q not empty)  u = DEQUEUE(Q)  for each v  Adj[u]  if color[v] = WHITE  then color[v] = GREY  d[v] = d[u] + 1; p[v] = u  ENQUEUE(Q, v);  color[u] = BLACK; 
  • 41. Data Structure and Algorithm         r s t u v w x y
  • 42. Data Structure and Algorithm   0      r s t u v w x y s Q:
  • 43. Data Structure and Algorithm 1  0 1     r s t u v w x y w Q: r
  • 44. Data Structure and Algorithm 1  0 1 2 2   r s t u v w x y r Q: t x
  • 45. Data Structure and Algorithm 1 2 0 1 2 2   r s t u v w x y Q: t x v
  • 46. Data Structure and Algorithm 1 2 0 1 2 2 3  r s t u v w x y Q: x v u
  • 47. Data Structure and Algorithm 1 2 0 1 2 2 3 3 r s t u v w x y Q: v u y
  • 48. Data Structure and Algorithm 1 2 0 1 2 2 3 3 r s t u v w x y Q: u y
  • 49. 1 2 0 1 2 2 3 3 r s t u v w x y Q: y
  • 50. 1 2 0 1 2 2 3 3 r s t u v w x y Q: Ø
  • 51. In computer science graph theory is used for the study of algorithms like: 1. Dijkstra's Algorithm 2. Prims's Algorithm 3. Kruskal's Algorithm Note: Above algorithms are used to find shortest path between two vertices.