SlideShare una empresa de Scribd logo
1 de 24
Data Structure
Graph
Graphs
● A data structure that consists of a set of nodes (vertices) and a
set of edges that relate the nodes to each other
● The set of edges describes relationships among the vertices .
● A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
2Graph
Examples of Graphs
● V={0,1,2,3,4}
● E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)}
Graph 3
0
1
4
2
3
When (x,y) is an edge,
we say that x is adjacent to y, and y
is adjacent from x.
0 is adjacent to 1.
1 is not adjacent to 0.
2 is adjacent from 1.
Directed vs. Undirected Graphs
● Undirected edge has no orientation (no arrow head)
● Directed edge has an orientation (has an arrow head)
● Undirected graph – all edges are undirected
● Directed graph – all edges are directed
u v
directed edge
u v
undirected edge
4Graph
Weighted graph:
-a graph in which each edge carries a value
5Graph
Directed graph Undirected graph
Directed Graph
● Directed edge (i, j) is incident to vertex j and
incident from vertex i
● Vertex i is adjacent to vertex j, and vertex j is
adjacent from vertex i
6Graph
Graph terminology
● Adjacent nodes: two nodes are adjacent if they are
connected by an edge
● Path: a sequence of vertices that connect two
nodes in a graph
● A simple path is a path in which all vertices, except
possibly in the first and last, are different.
● Complete graph: a graph in which every vertex is
directly connected to every other vertex
5 is adjacent to 7
7 is adjacent from
5
7Graph
Continued…
● A cycle is a simple path with the same start and
end vertex.
● The degree of vertex i is the no. of edges incident on
vertex i.
e.g., degree(2) = 2, degree(5) = 3, degree(3) = 1
Graph 8
Continued…
Undirected graphs are connected if there is a path
between any two vertices
Directed graphs are strongly connected if there is a path
from any one vertex to any other
Directed graphs are weakly connected if there is a path
between any two vertices, ignoring direction
A complete graph has an edge between every pair of
vertices
9Graph
Continued…
● Loops: edges that connect a vertex to itself
● Paths: sequences of vertices p0, p1, … pm such that
each adjacent pair of vertices are connected by an
edge
● A simple path is a path in which all vertices, except
possibly in the first and last, are different.
● Multiple Edges: two nodes may be connected by >1
edge
● Simple Graphs: have no loops and no multiple edges
10Graph
Graph Properties
Number of Edges – Undirected Graph
● The no. of possible pairs in an n vertex graph is
n*(n-1)
● Since edge (u,v) is the same as edge (v,u), the
number of edges in an undirected graph is n*(n-
1)/2.
Graph 11
Number of Edges - Directed Graph
● The no. of possible pairs in an n vertex graph is
n*(n-1)
● Since edge (u,v) is not the same as edge (v,u), the
number of edges in a directed graph is n*(n-1)
● Thus, the number of edges in a directed graph is ≤
n*(n-1)
Graph 12
Graph 13
• In-degree of vertex i is the number of edges incident to i (i.e.,
the number of incoming edges).
e.g., indegree(2) = 1, indegree(8) = 0
• Out-degree of vertex i is the number of edges incident from i
(i.e., the number of outgoing edges).
e.g., outdegree(2) = 1, outdegree(8) = 2
Graph Representation
● For graphs to be computationally useful, they have to
be conveniently represented in programs
● There are two computer representations of graphs:
● Adjacency matrix representation
● Adjacency lists representation
Graph 14
● Adjacency Matrix
● A square grid of boolean values
● If the graph contains N vertices, then the grid contains
N rows and N columns
● For two vertices numbered I and J, the element at row I
and column J is true if there is an edge from I to J,
otherwise false
Graph 15
Adjacency Matrix
Graph 16
1
2
3
0
4
0 1 2 3 4
0 false false true false false
1 false false false true false
2 false true false false true
3 false false false false false
4 false false false true false
Adjacency Matrix
1
43
5
2
17Graph
L23 18
Adjacency Matrix
-Directed Multigraphs
A:
1
2
34
Adjacency Lists Representation
● A graph of n nodes is represented by a one-
dimensional array L of linked lists, where
● L[i] is the linked list containing all the nodes adjacent
from node i.
● The nodes in the list L[i] are in no particular order
Graph 19
Graph
Graphs: Adjacency List
● Adjacency list: for each vertex v ∈ V, store a list of
vertices adjacent to v
● Example:
● Adj[1] = {2,3}
● Adj[2] = {3}
● Adj[3] = {}
● Adj[4] = {3}
● Variation: can also keep
a list of edges coming into vertex
1
2 4
3
20
Graphs: Adjacency List
● How much storage is required?
● The degree of a vertex v = # incident edges
● Directed graphs have in-degree, out-degree
● For directed graphs, # of items in adjacency lists is
Σ out-degree(v) = |E|
For undirected graphs, # items in adjacency lists is
Σ degree(v) = 2 |E|
● So: Adjacency lists take O(V+E) storage
21Graph
Implementing Graphs
Implementing Graphs
● (a) A weighted undirected graph and
(b) its adjacency list
Thank you!!!!
24Graph

Más contenido relacionado

La actualidad más candente

Graph representation
Graph representationGraph representation
Graph representationTech_MX
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure pptNalinNishant3
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked ListNinad Mankar
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
trees in data structure
trees in data structure trees in data structure
trees in data structure shameen khan
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and boundAbhishek Singh
 

La actualidad más candente (20)

Linked list
Linked listLinked list
Linked list
 
Graph representation
Graph representationGraph representation
Graph representation
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Hashing
HashingHashing
Hashing
 
Sorting
SortingSorting
Sorting
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
single linked list
single linked listsingle linked list
single linked list
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Linked list
Linked listLinked list
Linked list
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 

Similar a Graph in data structure

DATA STRUCTURES unit 4.pptx
DATA STRUCTURES unit 4.pptxDATA STRUCTURES unit 4.pptx
DATA STRUCTURES unit 4.pptxShivamKrPathak
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphsssuser034ce1
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphsssuser034ce1
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringSaurabh Kaushik
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theoryArvindBorge
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxbashirabdullah789
 
Elements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxmiki304759
 
Graph in Discrete mathemaetics.pptx
Graph in Discrete mathemaetics.pptxGraph in Discrete mathemaetics.pptx
Graph in Discrete mathemaetics.pptxujjwalmatoliya
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptxARVIND SARDAR
 

Similar a Graph in data structure (20)

Graphs
GraphsGraphs
Graphs
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
DATA STRUCTURES unit 4.pptx
DATA STRUCTURES unit 4.pptxDATA STRUCTURES unit 4.pptx
DATA STRUCTURES unit 4.pptx
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
Basics of graph
Basics of graphBasics of graph
Basics of graph
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Graphs
GraphsGraphs
Graphs
 
graph theory
graph theorygraph theory
graph theory
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theory
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
Elements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptx
 
09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf
 
Chapter 1
Chapter   1Chapter   1
Chapter 1
 
Graph in Discrete mathemaetics.pptx
Graph in Discrete mathemaetics.pptxGraph in Discrete mathemaetics.pptx
Graph in Discrete mathemaetics.pptx
 
Graphs.pdf
Graphs.pdfGraphs.pdf
Graphs.pdf
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 

Último

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
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_.pdfSherif Taha
 
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.pdfPoh-Sun Goh
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
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
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
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.pptxAreebaZafar22
 

Último (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . 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
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
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
 
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
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
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...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
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
 

Graph in data structure

  • 2. Graphs ● A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other ● The set of edges describes relationships among the vertices . ● A graph G is defined as follows: G=(V,E) V(G): a finite, nonempty set of vertices E(G): a set of edges (pairs of vertices) 2Graph
  • 3. Examples of Graphs ● V={0,1,2,3,4} ● E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)} Graph 3 0 1 4 2 3 When (x,y) is an edge, we say that x is adjacent to y, and y is adjacent from x. 0 is adjacent to 1. 1 is not adjacent to 0. 2 is adjacent from 1.
  • 4. Directed vs. Undirected Graphs ● Undirected edge has no orientation (no arrow head) ● Directed edge has an orientation (has an arrow head) ● Undirected graph – all edges are undirected ● Directed graph – all edges are directed u v directed edge u v undirected edge 4Graph
  • 5. Weighted graph: -a graph in which each edge carries a value 5Graph
  • 6. Directed graph Undirected graph Directed Graph ● Directed edge (i, j) is incident to vertex j and incident from vertex i ● Vertex i is adjacent to vertex j, and vertex j is adjacent from vertex i 6Graph
  • 7. Graph terminology ● Adjacent nodes: two nodes are adjacent if they are connected by an edge ● Path: a sequence of vertices that connect two nodes in a graph ● A simple path is a path in which all vertices, except possibly in the first and last, are different. ● Complete graph: a graph in which every vertex is directly connected to every other vertex 5 is adjacent to 7 7 is adjacent from 5 7Graph
  • 8. Continued… ● A cycle is a simple path with the same start and end vertex. ● The degree of vertex i is the no. of edges incident on vertex i. e.g., degree(2) = 2, degree(5) = 3, degree(3) = 1 Graph 8
  • 9. Continued… Undirected graphs are connected if there is a path between any two vertices Directed graphs are strongly connected if there is a path from any one vertex to any other Directed graphs are weakly connected if there is a path between any two vertices, ignoring direction A complete graph has an edge between every pair of vertices 9Graph
  • 10. Continued… ● Loops: edges that connect a vertex to itself ● Paths: sequences of vertices p0, p1, … pm such that each adjacent pair of vertices are connected by an edge ● A simple path is a path in which all vertices, except possibly in the first and last, are different. ● Multiple Edges: two nodes may be connected by >1 edge ● Simple Graphs: have no loops and no multiple edges 10Graph
  • 11. Graph Properties Number of Edges – Undirected Graph ● The no. of possible pairs in an n vertex graph is n*(n-1) ● Since edge (u,v) is the same as edge (v,u), the number of edges in an undirected graph is n*(n- 1)/2. Graph 11
  • 12. Number of Edges - Directed Graph ● The no. of possible pairs in an n vertex graph is n*(n-1) ● Since edge (u,v) is not the same as edge (v,u), the number of edges in a directed graph is n*(n-1) ● Thus, the number of edges in a directed graph is ≤ n*(n-1) Graph 12
  • 13. Graph 13 • In-degree of vertex i is the number of edges incident to i (i.e., the number of incoming edges). e.g., indegree(2) = 1, indegree(8) = 0 • Out-degree of vertex i is the number of edges incident from i (i.e., the number of outgoing edges). e.g., outdegree(2) = 1, outdegree(8) = 2
  • 14. Graph Representation ● For graphs to be computationally useful, they have to be conveniently represented in programs ● There are two computer representations of graphs: ● Adjacency matrix representation ● Adjacency lists representation Graph 14
  • 15. ● Adjacency Matrix ● A square grid of boolean values ● If the graph contains N vertices, then the grid contains N rows and N columns ● For two vertices numbered I and J, the element at row I and column J is true if there is an edge from I to J, otherwise false Graph 15
  • 16. Adjacency Matrix Graph 16 1 2 3 0 4 0 1 2 3 4 0 false false true false false 1 false false false true false 2 false true false false true 3 false false false false false 4 false false false true false
  • 18. L23 18 Adjacency Matrix -Directed Multigraphs A: 1 2 34
  • 19. Adjacency Lists Representation ● A graph of n nodes is represented by a one- dimensional array L of linked lists, where ● L[i] is the linked list containing all the nodes adjacent from node i. ● The nodes in the list L[i] are in no particular order Graph 19
  • 20. Graph Graphs: Adjacency List ● Adjacency list: for each vertex v ∈ V, store a list of vertices adjacent to v ● Example: ● Adj[1] = {2,3} ● Adj[2] = {3} ● Adj[3] = {} ● Adj[4] = {3} ● Variation: can also keep a list of edges coming into vertex 1 2 4 3 20
  • 21. Graphs: Adjacency List ● How much storage is required? ● The degree of a vertex v = # incident edges ● Directed graphs have in-degree, out-degree ● For directed graphs, # of items in adjacency lists is Σ out-degree(v) = |E| For undirected graphs, # items in adjacency lists is Σ degree(v) = 2 |E| ● So: Adjacency lists take O(V+E) storage 21Graph
  • 23. Implementing Graphs ● (a) A weighted undirected graph and (b) its adjacency list