SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
Data Structure
Graphs
Preston University Kohat
Hamza Javed
BCS 4th
2121-115001
Graphs
Introduction
• Graphs are a generalization of trees
• Nodes or vertices
• Edges or arcs
• Two kinds of graphs
• Directed
• Undirected
3
Introduction: Formal Definition
• A graph G = (V,E) consists of a finite set of vertices, V, and a finite set
of edges E.
• Each edge is a pair (v,w) where v, w  V
4
Introduction: Formal Definition
• A directed graph, or digraph, is a graph in which the edges are
ordered pairs
• (v, w) ≠ (w, v)
• An undirected graph is a graph in which the edges are unordered
pairs
• (v, w) == (w, v)
5
Introduction: Directed Graphs
• In a directed graph, the edges are arrows.
• Directed graphs show the flow from one node to another and not vise
versa.
6
Introduction: Undirected Graphs
• In a Undirected graph, the edges are lines.
• UnDirected graphs show a relationship between two nodes.
7
Terminology
• In the directed graph above, b is adjacent to a
because (a, b)  E. Note that a is not adjacent to b.
• A is a predecessor of node B
• B is a successor of node A
• The source of the edge is node A, the target is node B
8
Terminology
• In the undirected graph above, a and b are adjacent
because (a,b)  E. a and b are called neighbors.
9
Terminology
• A path is a sequence of vertices w1, w2,…wn such that
(wi, wi+1)  E, 1 <= i < n, and each vertex is unique
except that the path may start and end on the same
vertex
• The length of the path is the number of edges along
the path
10
Terminology
• An acyclic path is a path which does not follow a
sequence.
• A cyclic path is a path such that
• There are at least two vertices on the path
• w1 = wn (path starts and ends at same vertex)
• And also maintains the sequence
11
Test Your Knowledge
Cyclic or Acyclic?
12
Terminology
• A directed graph that has no cyclic paths is called a
DAG (a Directed Acyclic Graph).
• An undirected graph that has an edge between every
pair of vertices is called a complete graph.
Note: A directed graph can also be
a complete graph; in that case, there
must be an edge from every vertex
to every other vertex.
13
Test Your Knowledge
Complete, or “Acomplete” (Not Complete)
14
Test Your Knowledge
Complete, or “Acomplete” (Not Complete)
15
Terminology
• An undirected graph is connected if a path exists from
every vertex to every other vertex
• A directed graph is strongly connected if a path exists
from every vertex to every other vertex
• A directed graph is weakly connected if a path exists
from every vertex to every other vertex, disregarding
the direction of the edge
16
Terminology
• A graph is known as a weighted graph if a weight or
metric is associated with each edge.
17
Representation of Graph
18
Following are the representation of a graph
• Adjacency Matrix
• Adjacency List
There are other representations also like, Incidence Matrix and
Incidence List. The choice of the graph representation is situation
specific. It totally depends on the type of operations to be performed
and ease of use.
19
Adjacency Matrix
• Adjacency Matrix is a 2D array of size V x V where V is the number of
vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1
indicates that there is an edge from vertex i to vertex j. Adjacency
matrix for undirected graph is always symmetric. Adjacency Matrix is
also used to represent weighted graphs. If adj[i][j] = w, then there is
an edge from vertex i to vertex j with weight w.
20
Adjacency Matrix
21
Adjacency List
• An array of linked lists is used. Size of the array is equal to number of
vertices. Let the array be array[]. An entry array[i] represents the
linked list of vertices adjacent to the ith vertex. This representation
can also be used to represent a weighted graph. The weights of edges
can be stored in nodes of linked lists. Following is adjacency list
representation of the above graph.
22
Adjacency List
23
No
de
Adjacency List
A
B
C
D
E
B,C,D
C
C,E
C
a) Graphs G
b) Adjacency lists of G
24
25
Uses For Graphs
26
Uses for Graphs
• Computer network: The set of vertices V represents the set of
computers in the network. There is an edge (u, v) if and only if there
is a direct communication link between the computers corresponding
to u and v.
27
Uses for Graphs
• Two-Player Game Tree: All of the possibilities in a board game like
chess can be represented in a graph. Each vertex stands for one
possible board position. (For chess, this is a very big graph!)
28
Social Media (Facebook)
29
Websites
30
Intercity Road Network
31
32

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Graphs in datastructures
Graphs in datastructuresGraphs in datastructures
Graphs in datastructures
 
Linked List
Linked ListLinked List
Linked List
 
Red black tree
Red black treeRed black tree
Red black tree
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Kruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmKruskal & Prim's Algorithm
Kruskal & Prim's Algorithm
 
Lecture8 data structure(graph)
Lecture8 data structure(graph)Lecture8 data structure(graph)
Lecture8 data structure(graph)
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theory
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
 
Graph Theory Introduction
Graph Theory IntroductionGraph Theory Introduction
Graph Theory Introduction
 
Graphs data structures
Graphs data structuresGraphs data structures
Graphs data structures
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graph theory
Graph theory Graph theory
Graph theory
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 

Andere mochten auch

Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...sethuraman R
 
Graph theory 1
Graph theory 1Graph theory 1
Graph theory 1Tech_MX
 
Apache Flink & Graph Processing
Apache Flink & Graph ProcessingApache Flink & Graph Processing
Apache Flink & Graph ProcessingVasia Kalavri
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureZaid Shabbir
 
Batch and Stream Graph Processing with Apache Flink
Batch and Stream Graph Processing with Apache FlinkBatch and Stream Graph Processing with Apache Flink
Batch and Stream Graph Processing with Apache FlinkVasia Kalavri
 
Graph data structure
Graph data structureGraph data structure
Graph data structureTech_MX
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its typesRameesha Sadaqat
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)Arvind Devaraj
 
Why apache Flink is the 4G of Big Data Analytics Frameworks
Why apache Flink is the 4G of Big Data Analytics FrameworksWhy apache Flink is the 4G of Big Data Analytics Frameworks
Why apache Flink is the 4G of Big Data Analytics FrameworksSlim Baltagi
 

Andere mochten auch (15)

Graphs data Structure
Graphs data StructureGraphs data Structure
Graphs data Structure
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
 
Graph theory 1
Graph theory 1Graph theory 1
Graph theory 1
 
Apache Flink & Graph Processing
Apache Flink & Graph ProcessingApache Flink & Graph Processing
Apache Flink & Graph Processing
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Batch and Stream Graph Processing with Apache Flink
Batch and Stream Graph Processing with Apache FlinkBatch and Stream Graph Processing with Apache Flink
Batch and Stream Graph Processing with Apache Flink
 
Graph data structure
Graph data structureGraph data structure
Graph data structure
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
 
Types of graphs
Types of graphsTypes of graphs
Types of graphs
 
Graphs ppt
Graphs pptGraphs ppt
Graphs ppt
 
Why apache Flink is the 4G of Big Data Analytics Frameworks
Why apache Flink is the 4G of Big Data Analytics FrameworksWhy apache Flink is the 4G of Big Data Analytics Frameworks
Why apache Flink is the 4G of Big Data Analytics Frameworks
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Ähnlich wie Graphs in data structure (20)

Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
Graph 1
Graph 1Graph 1
Graph 1
 
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
 
UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
Graphs Algorithms
Graphs AlgorithmsGraphs Algorithms
Graphs Algorithms
 
Graphs (Models & Terminology)
Graphs (Models & Terminology)Graphs (Models & Terminology)
Graphs (Models & Terminology)
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graph
GraphGraph
Graph
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 
Graph
GraphGraph
Graph
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
Basics of graph
Basics of graphBasics of graph
Basics of graph
 
ppt 1.pptx
ppt 1.pptxppt 1.pptx
ppt 1.pptx
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 

Kürzlich hochgeladen

Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialJoão Esperancinha
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...itnewsafrica
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 

Kürzlich hochgeladen (20)

Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorial
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 

Graphs in data structure

  • 1. Data Structure Graphs Preston University Kohat Hamza Javed BCS 4th 2121-115001
  • 3. Introduction • Graphs are a generalization of trees • Nodes or vertices • Edges or arcs • Two kinds of graphs • Directed • Undirected 3
  • 4. Introduction: Formal Definition • A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges E. • Each edge is a pair (v,w) where v, w  V 4
  • 5. Introduction: Formal Definition • A directed graph, or digraph, is a graph in which the edges are ordered pairs • (v, w) ≠ (w, v) • An undirected graph is a graph in which the edges are unordered pairs • (v, w) == (w, v) 5
  • 6. Introduction: Directed Graphs • In a directed graph, the edges are arrows. • Directed graphs show the flow from one node to another and not vise versa. 6
  • 7. Introduction: Undirected Graphs • In a Undirected graph, the edges are lines. • UnDirected graphs show a relationship between two nodes. 7
  • 8. Terminology • In the directed graph above, b is adjacent to a because (a, b)  E. Note that a is not adjacent to b. • A is a predecessor of node B • B is a successor of node A • The source of the edge is node A, the target is node B 8
  • 9. Terminology • In the undirected graph above, a and b are adjacent because (a,b)  E. a and b are called neighbors. 9
  • 10. Terminology • A path is a sequence of vertices w1, w2,…wn such that (wi, wi+1)  E, 1 <= i < n, and each vertex is unique except that the path may start and end on the same vertex • The length of the path is the number of edges along the path 10
  • 11. Terminology • An acyclic path is a path which does not follow a sequence. • A cyclic path is a path such that • There are at least two vertices on the path • w1 = wn (path starts and ends at same vertex) • And also maintains the sequence 11
  • 12. Test Your Knowledge Cyclic or Acyclic? 12
  • 13. Terminology • A directed graph that has no cyclic paths is called a DAG (a Directed Acyclic Graph). • An undirected graph that has an edge between every pair of vertices is called a complete graph. Note: A directed graph can also be a complete graph; in that case, there must be an edge from every vertex to every other vertex. 13
  • 14. Test Your Knowledge Complete, or “Acomplete” (Not Complete) 14
  • 15. Test Your Knowledge Complete, or “Acomplete” (Not Complete) 15
  • 16. Terminology • An undirected graph is connected if a path exists from every vertex to every other vertex • A directed graph is strongly connected if a path exists from every vertex to every other vertex • A directed graph is weakly connected if a path exists from every vertex to every other vertex, disregarding the direction of the edge 16
  • 17. Terminology • A graph is known as a weighted graph if a weight or metric is associated with each edge. 17
  • 19. Following are the representation of a graph • Adjacency Matrix • Adjacency List There are other representations also like, Incidence Matrix and Incidence List. The choice of the graph representation is situation specific. It totally depends on the type of operations to be performed and ease of use. 19
  • 20. Adjacency Matrix • Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w. 20
  • 22. Adjacency List • An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be array[]. An entry array[i] represents the linked list of vertices adjacent to the ith vertex. This representation can also be used to represent a weighted graph. The weights of edges can be stored in nodes of linked lists. Following is adjacency list representation of the above graph. 22
  • 25. 25
  • 27. Uses for Graphs • Computer network: The set of vertices V represents the set of computers in the network. There is an edge (u, v) if and only if there is a direct communication link between the computers corresponding to u and v. 27
  • 28. Uses for Graphs • Two-Player Game Tree: All of the possibilities in a board game like chess can be represented in a graph. Each vertex stands for one possible board position. (For chess, this is a very big graph!) 28
  • 32. 32