SlideShare a Scribd company logo
1 of 21
Graph Algorithms
Kasun Ranga Wijeweera
(Email: krw19870829@gmail.com)
What is a Graph?
• Intuitively, a graph is a collection of vertices (or nodes) and
the connections between them
• Generally, no restriction is imposed on the number of vertices
in the graph or on the number of connections one vertex can
have to other vertices
A Simple Graph
• A simple graph G = (V, E) consists of a nonempty set V of
vertices and a possibly empty set E of edges, each edge being a
set of two vertices from V
• |V| = Number of vertices
• |E| = Number of edges
A Directed Graph
• A directed graph, or digraph, G = (V, E) consists of a
nonempty set V of vertices and a set E of edges (also called
arcs), where each edge is pair of vertices from V
• The difference is that one edge of a simple graph is of the
form {vi, vj}, and in this case, (vi, vj) != (vj , vi)
A Multi Graph
• Above definitions are restrictive in that they do not allow for
two vertices to have more than one edge
• A multi graph is graph in which two vertices can be joined by
multiple edges
• Formal Definition: A multi graph G = (V, E, f) is composed of
a set of vertices V, a set of edges E, and a function
f: E{(vi, vj): (vi, vj in V) and (vi != vj)}
A Pseudo Graph
• A pseudo graph is a multi graph with the condition vi != vj
removed, which allows for loops to occur
• In a pseudo graph, a vertex can be joined with itself by an edge
A Path
• A path from v1 to vn is a sequence of edges edge(v1v2),
edge(v2v3), . . . , edge(vn-1vn) and is denoted as path v1, v2, v3, .
. . , vn-1, vn
• If v1 = v2 and no edge is repeated, then the path is called a
circuit
• If all vertices in a circuit are different, then it is called a cycle
A Weighted Graph
• A graph is called a weighted graph if each edge has an
assigned number
• Depending on the context in which such graphs are used, the
number assigned to an edge is called it weight, cost, distance,
length, or some other name
A Complete Graph
• A graph with n vertices is called complete and is denoted K n if
for each pair of distinct vertices there is exactly one edge
connecting them
• The number of edges in such a graph
|E| = |V|*(|V| - 1)*0.5
A Sub Graph
• A sub graph G’ of graph G = (V, E) is a graph (V’, E’) such
that V’ is a subset of V and E’ is a subset of E
• A sub graph induced by vertices V’ is a graph (V’, E’) such
that and edge e in E if e in E’
Adjacent? Incident?
• Two vertices vi and vj are called adjacent if the edge(vivj) is in
E
• Such an edge is called incident with the vertices vi and vj
The Degree of a Vertex
• The degree of a vertex v, deg(v), is the number of edges
incident with v
• If deg(v) = 0, then v is called an isolated vertex
Adjacency Matrix
• An adjacency matrix of graph G = (V, E) is a binary |V|*|V|
matrix such that each entry of this matrix
a ij
1; if there exists an edge(vivj)
0; otherwise
Incidence Matrix
• An incidence matrix of graph G = (V, E) is a |V|*|E| matrix
such that each entry of this matrix
a ij
1; if edge ej is incident with vertex vi
0; otherwise
Graph Traversals
• Traversing a graph consists of visiting each vertex only one
time
• Graphs may include cycles that can cause infinite loops
• To prevent infinite loops each visited vertex can be marked to
avoid revisiting it
• Graphs can have isolated vertices
• To visit those isolated vertices special mechanisms are needed
• The Depth First Search algorithm is a well known algorithm
for traversing graphs
Depth First Search Algorithm
• Each vertex v is visited and then each unvisited vertex
adjacent to v is visited
• If a vertex v has no adjacent vertices or all of its adjacent
vertices have been visited, we backtrack to the predecessor of
v
• The traversal is finished if this visiting and backtracking
process leads to the first vertex where the traversal started
• If there is still some unvisited vertices in the graph, the
traversal continues restarting for one of the unvisited vertices
• The algorithm assigns a unique number to each accessed
vertex so that vertices are now renumbered
Depth First Search Algorithm
depthFirstSearch()
for all vertices v
num(v) = 0;
edges = null;
i = 1;
while there is a vertex v such that num(v) is 0
DFS(v);
output edges;
Depth First Search Algorithm
DFS(v)
num(v) = i++;
for all vertices u adjacent to v
if num(u) is 0
attach edge(uv) to edges;
DFS(u);
Reference
Any Questions?
Thank You!

More Related Content

What's hot

Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithmami_01
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
 
Graph representation
Graph representationGraph representation
Graph representationTech_MX
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structureAbrish06
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithmmansab MIRZA
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theoryChuckie Balbuena
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Applications of graphs
Applications of graphsApplications of graphs
Applications of graphsTech_MX
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtrackingmandlapure
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage CollectionEelco Visser
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen ProblemSukrit Gupta
 

What's hot (20)

Graph colouring
Graph colouringGraph colouring
Graph colouring
 
Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithm
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Graph representation
Graph representationGraph representation
Graph representation
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theory
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Applications of graphs
Applications of graphsApplications of graphs
Applications of graphs
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage Collection
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
graph theory
graph theory graph theory
graph theory
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 

Similar to Graphs Algorithms (20)

14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
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
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Graph in Discrete mathemaetics.pptx
Graph in Discrete mathemaetics.pptxGraph in Discrete mathemaetics.pptx
Graph in Discrete mathemaetics.pptx
 
Basic graph theory
Basic graph theoryBasic graph theory
Basic graph theory
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Graph 1
Graph 1Graph 1
Graph 1
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Graph
GraphGraph
Graph
 
Graph Theory Introduction
Graph Theory IntroductionGraph Theory Introduction
Graph Theory Introduction
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Graph representation
Graph representationGraph representation
Graph representation
 
UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 

More from Kasun Ranga Wijeweera

Algorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a PolygonAlgorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a PolygonKasun Ranga Wijeweera
 
Digital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing AlgorithmDigital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing AlgorithmKasun Ranga Wijeweera
 
Getting Started with Visual Basic Programming
Getting Started with Visual Basic ProgrammingGetting Started with Visual Basic Programming
Getting Started with Visual Basic ProgrammingKasun Ranga Wijeweera
 
Variables in Visual Basic Programming
Variables in Visual Basic ProgrammingVariables in Visual Basic Programming
Variables in Visual Basic ProgrammingKasun Ranga Wijeweera
 
Conditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic ProgrammingConditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic ProgrammingKasun Ranga Wijeweera
 
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]Kasun Ranga Wijeweera
 

More from Kasun Ranga Wijeweera (20)

Decorator Design Pattern in C#
Decorator Design Pattern in C#Decorator Design Pattern in C#
Decorator Design Pattern in C#
 
Singleton Design Pattern in C#
Singleton Design Pattern in C#Singleton Design Pattern in C#
Singleton Design Pattern in C#
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
Algorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a PolygonAlgorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a Polygon
 
Geometric Transformations II
Geometric Transformations IIGeometric Transformations II
Geometric Transformations II
 
Geometric Transformations I
Geometric Transformations IGeometric Transformations I
Geometric Transformations I
 
Introduction to Polygons
Introduction to PolygonsIntroduction to Polygons
Introduction to Polygons
 
Bresenham Line Drawing Algorithm
Bresenham Line Drawing AlgorithmBresenham Line Drawing Algorithm
Bresenham Line Drawing Algorithm
 
Digital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing AlgorithmDigital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing Algorithm
 
Loops in Visual Basic: Exercises
Loops in Visual Basic: ExercisesLoops in Visual Basic: Exercises
Loops in Visual Basic: Exercises
 
Conditional Logic: Exercises
Conditional Logic: ExercisesConditional Logic: Exercises
Conditional Logic: Exercises
 
Getting Started with Visual Basic Programming
Getting Started with Visual Basic ProgrammingGetting Started with Visual Basic Programming
Getting Started with Visual Basic Programming
 
CheckBoxes and RadioButtons
CheckBoxes and RadioButtonsCheckBoxes and RadioButtons
CheckBoxes and RadioButtons
 
Variables in Visual Basic Programming
Variables in Visual Basic ProgrammingVariables in Visual Basic Programming
Variables in Visual Basic Programming
 
Loops in Visual Basic Programming
Loops in Visual Basic ProgrammingLoops in Visual Basic Programming
Loops in Visual Basic Programming
 
Conditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic ProgrammingConditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic Programming
 
Assignment for Variables
Assignment for VariablesAssignment for Variables
Assignment for Variables
 
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]
 
Assignment for Events
Assignment for EventsAssignment for Events
Assignment for Events
 
Mastering Arrays Assignment
Mastering Arrays AssignmentMastering Arrays Assignment
Mastering Arrays Assignment
 

Recently uploaded

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 

Recently uploaded (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Graphs Algorithms

  • 1. Graph Algorithms Kasun Ranga Wijeweera (Email: krw19870829@gmail.com)
  • 2. What is a Graph? • Intuitively, a graph is a collection of vertices (or nodes) and the connections between them • Generally, no restriction is imposed on the number of vertices in the graph or on the number of connections one vertex can have to other vertices
  • 3. A Simple Graph • A simple graph G = (V, E) consists of a nonempty set V of vertices and a possibly empty set E of edges, each edge being a set of two vertices from V • |V| = Number of vertices • |E| = Number of edges
  • 4. A Directed Graph • A directed graph, or digraph, G = (V, E) consists of a nonempty set V of vertices and a set E of edges (also called arcs), where each edge is pair of vertices from V • The difference is that one edge of a simple graph is of the form {vi, vj}, and in this case, (vi, vj) != (vj , vi)
  • 5. A Multi Graph • Above definitions are restrictive in that they do not allow for two vertices to have more than one edge • A multi graph is graph in which two vertices can be joined by multiple edges • Formal Definition: A multi graph G = (V, E, f) is composed of a set of vertices V, a set of edges E, and a function f: E{(vi, vj): (vi, vj in V) and (vi != vj)}
  • 6. A Pseudo Graph • A pseudo graph is a multi graph with the condition vi != vj removed, which allows for loops to occur • In a pseudo graph, a vertex can be joined with itself by an edge
  • 7. A Path • A path from v1 to vn is a sequence of edges edge(v1v2), edge(v2v3), . . . , edge(vn-1vn) and is denoted as path v1, v2, v3, . . . , vn-1, vn • If v1 = v2 and no edge is repeated, then the path is called a circuit • If all vertices in a circuit are different, then it is called a cycle
  • 8. A Weighted Graph • A graph is called a weighted graph if each edge has an assigned number • Depending on the context in which such graphs are used, the number assigned to an edge is called it weight, cost, distance, length, or some other name
  • 9. A Complete Graph • A graph with n vertices is called complete and is denoted K n if for each pair of distinct vertices there is exactly one edge connecting them • The number of edges in such a graph |E| = |V|*(|V| - 1)*0.5
  • 10. A Sub Graph • A sub graph G’ of graph G = (V, E) is a graph (V’, E’) such that V’ is a subset of V and E’ is a subset of E • A sub graph induced by vertices V’ is a graph (V’, E’) such that and edge e in E if e in E’
  • 11. Adjacent? Incident? • Two vertices vi and vj are called adjacent if the edge(vivj) is in E • Such an edge is called incident with the vertices vi and vj
  • 12. The Degree of a Vertex • The degree of a vertex v, deg(v), is the number of edges incident with v • If deg(v) = 0, then v is called an isolated vertex
  • 13. Adjacency Matrix • An adjacency matrix of graph G = (V, E) is a binary |V|*|V| matrix such that each entry of this matrix a ij 1; if there exists an edge(vivj) 0; otherwise
  • 14. Incidence Matrix • An incidence matrix of graph G = (V, E) is a |V|*|E| matrix such that each entry of this matrix a ij 1; if edge ej is incident with vertex vi 0; otherwise
  • 15. Graph Traversals • Traversing a graph consists of visiting each vertex only one time • Graphs may include cycles that can cause infinite loops • To prevent infinite loops each visited vertex can be marked to avoid revisiting it • Graphs can have isolated vertices • To visit those isolated vertices special mechanisms are needed • The Depth First Search algorithm is a well known algorithm for traversing graphs
  • 16. Depth First Search Algorithm • Each vertex v is visited and then each unvisited vertex adjacent to v is visited • If a vertex v has no adjacent vertices or all of its adjacent vertices have been visited, we backtrack to the predecessor of v • The traversal is finished if this visiting and backtracking process leads to the first vertex where the traversal started • If there is still some unvisited vertices in the graph, the traversal continues restarting for one of the unvisited vertices • The algorithm assigns a unique number to each accessed vertex so that vertices are now renumbered
  • 17. Depth First Search Algorithm depthFirstSearch() for all vertices v num(v) = 0; edges = null; i = 1; while there is a vertex v such that num(v) is 0 DFS(v); output edges;
  • 18. Depth First Search Algorithm DFS(v) num(v) = i++; for all vertices u adjacent to v if num(u) is 0 attach edge(uv) to edges; DFS(u);