SlideShare una empresa de Scribd logo
1 de 103
Descargar para leer sin conexión
Analysis and Design of Algorithms
Graph Algorithms
Analysis and Design of Algorithms
Graph
Directed vs Undirected Graph
Acyclic vs Cyclic Graph
Backedge
Search vs Traversal
Breadth First Traversal
Depth First Traversal
Detect Cycle in a Directed Graph
Analysis and Design of Algorithms
 Graph data structure consists of a
finite set of vertices or nodes. The
interconnected objects are
represented by points termed as
vertices, and the links that connect
the vertices are called edges.
A
B C
D E
F
Node or Vertices
Edge
Analysis and Design of Algorithms
 Vertex: Each node of the graph is
represented as a vertex.
 V={A,B,C,D,E,F}
 Edge: Edge represents a path
between two vertices or a line
between two vertices.
 E={AB,AC,BD,BE,CE,DE,DF,EF}
A
B C
D E
F
Node or Vertices
Edge
Analysis and Design of Algorithms
A
B C
A
B C
Directed Graph Undirected Graph
Analysis and Design of Algorithms
A
B C
Acyclic Graph Cyclic Graph
A
B C
Analysis and Design of Algorithms
Backedge is an edge that is from node to itself
(selfloop), or one to its ancestor.
A
B
A
Analysis and Design of Algorithms
Search: Look for a given node
Traversal: Always visit all nodes
Analysis and Design of Algorithms
Breadth First Traversal
Analysis and Design of Algorithms
Breadth First Traversal (BFT) algorithm traverses all nodes
on graph in a breadthward motion and uses a queue to
remember to get the next vertex.
Analysis and Design of Algorithms
Layers A
B C
D E
F
Layer1
Layer2
Layer3
Layer4
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print:
A
B C
D E
F
0 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print:
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= A
 Print:
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A
A
B C
D E
F
1 1 1 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= B C
 Print: A
A
B C
D E
F
1 1 1 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= C
 Print: A B
A
B C
D E
F
1 1 1 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= C
 Print: A B
A
B C
D E
F
1 1 1 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= C
 Print: A B
A
B C
D E
F
1 1 1 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= C
 Print: A B
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= C D E
 Print: A B
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= D E
 Print: A B C
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= D E
 Print: A B C
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= E
 Print: A B C
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= E
 Print: A B C D
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= E
 Print: A B C D
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= E
 Print: A B C D
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= E F
 Print: A B C D
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= F
 Print: A B C D E
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= F
 Print: A B C D E
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= F
 Print: A B C D E
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A B C D E
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A B C D E F
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
Breadth First on Tree
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 V={0,1,2,3,4,5}
 E={01,02,13,14,24,34,35,45}
0
1 2
3 4
5
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 Another Example:
Analysis and Design of Algorithms
Depth First Traversal
Analysis and Design of Algorithms
Depth First Traversal (DFT) algorithm traverses a graph in
a depthward motion and uses a stack to remember to get
the next vertex to start a search.
Analysis and Design of Algorithms
depthward motion A
B C
D E
F
Analysis and Design of Algorithms
 Visited=
Stack=
 Print:
A
B C
D E
F
0 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
Stack=
 Print:
A
B C
D E
F
0 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
Stack=
 Print:
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
Stack=
 Print:
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A
A
B C
D E
F
1 1 0 0 0 0
A B C D E F
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A
A
B C
D E
F
1 1 0 0 0 0
A B C D E F
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B
A
B C
D E
F
1 1 0 0 0 0
A B C D E F
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B
A
B C
D E
F
1 1 0 0 0 0
A B C D E F
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B
A
B C
D E
F
1 1 0 1 0 0
A B C D E F
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B
A
B C
D E
F
1 1 0 1 0 0
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D
A
B C
D E
F
1 1 0 1 0 0
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D
A
B C
D E
F
1 1 0 1 0 0
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D
A
B C
D E
F
1 1 0 1 0 0
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D
A
B C
D E
F
1 1 0 1 1 0
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D
A
B C
D E
F
1 1 0 1 1 0
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E
A
B C
D E
F
1 1 0 1 1 0
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E
A
B C
D E
F
1 1 0 1 1 0
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E
A
B C
D E
F
1 1 0 1 1 0
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
C
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
Depth First on tree
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 V={0,1,2,3,4,5}
 E={01,02,13,14,24,34,35,45}
0
1 2
3 4
5
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 Another Example:
Analysis and Design of Algorithms
Given a directed graph, check whether the graph
contains a cycle or not. Your function should return
true if the given graph contains at least one cycle,
else return false.
Analysis and Design of Algorithms
Depth First Traversal can be used to detect cycle in a
Graph. There is a cycle in a graph only if there is a
back edge present in the graph.
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
0 0 0 0
A B C D
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
0 0 0 0
A B C D
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 0 0 0
A B C D
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 0 0 0
A B C D
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 0 0 0
A B C D
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 0 0
A B C D
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 0 0
A B C D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 0 0
A B C D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 1 0
A B C D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 1 0
A B C D
C
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 1 0
A B C D
C
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 1 0
A B C D
C
B
A
Analysis and Design of Algorithms
Graph contains cycle
A
C
B
D
Analysis and Design of Algorithms
Analysis and Design of Algorithms
Analysis and Design of Algorithms
facebook.com/mloey
mohamedloey@gmail.com
twitter.com/mloey
linkedin.com/in/mloey
mloey@fci.bu.edu.eg
mloey.github.io
Analysis and Design of Algorithms
www.YourCompany.com
© 2020 Companyname PowerPoint Business Theme. All Rights Reserved.
THANKS FOR
YOUR TIME

Más contenido relacionado

La actualidad más candente

Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 

La actualidad más candente (20)

Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
stack & queue
stack & queuestack & queue
stack & queue
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Max flow min cut
Max flow min cutMax flow min cut
Max flow min cut
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
chapter 1
chapter 1chapter 1
chapter 1
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 

Similar a Algorithms Lecture 7: Graph Algorithms

20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers
Computer Science Club
 

Similar a Algorithms Lecture 7: Graph Algorithms (20)

Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
graphs
graphsgraphs
graphs
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
 
Graph theory basics
Graph theory basicsGraph theory basics
Graph theory basics
 
lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.ppt
 
Graphs
GraphsGraphs
Graphs
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
Pre-Cal 40S March 1, 2009
Pre-Cal 40S March 1, 2009Pre-Cal 40S March 1, 2009
Pre-Cal 40S March 1, 2009
 
267 4 determinant and cross product-n
267 4 determinant and cross product-n267 4 determinant and cross product-n
267 4 determinant and cross product-n
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Graph theory
Graph theory Graph theory
Graph theory
 
Graph
GraphGraph
Graph
 
Graph Introduction.ppt
Graph Introduction.pptGraph Introduction.ppt
Graph Introduction.ppt
 
20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers
 
18 Basic Graph Algorithms
18 Basic Graph Algorithms18 Basic Graph Algorithms
18 Basic Graph Algorithms
 
Drawing Tools
Drawing ToolsDrawing Tools
Drawing Tools
 
Ai4 heuristic2
Ai4 heuristic2Ai4 heuristic2
Ai4 heuristic2
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 

Más de Mohamed Loey

Design of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer DiseasesDesign of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer Diseases
Mohamed Loey
 

Más de Mohamed Loey (20)

Lecture 6: Deep Learning Applications
Lecture 6: Deep Learning ApplicationsLecture 6: Deep Learning Applications
Lecture 6: Deep Learning Applications
 
Lecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network ModelsLecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network Models
 
Lecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning FrameworksLecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning Frameworks
 
Lecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural NetworksLecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural Networks
 
Lecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural NetworksLecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural Networks
 
Lecture 2: Artificial Neural Network
Lecture 2: Artificial Neural NetworkLecture 2: Artificial Neural Network
Lecture 2: Artificial Neural Network
 
Lecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer VisionLecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer Vision
 
Design of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer DiseasesDesign of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer Diseases
 
Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2
 
Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1
 
Algorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsAlgorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern Algorithms
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms II
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms II
 
Convolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep LearningConvolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep Learning
 
Deep Learning - Overview of my work II
Deep Learning - Overview of my work IIDeep Learning - Overview of my work II
Deep Learning - Overview of my work II
 
Computer Security Lecture 7: RSA
Computer Security Lecture 7: RSAComputer Security Lecture 7: RSA
Computer Security Lecture 7: RSA
 
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption StandardComputer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption Standard
 
Computer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary MaterialComputer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary Material
 
PMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration ManagementPMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration Management
 
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption StandardComputer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Último (20)

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
 
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
 
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
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
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...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
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...
 
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Ữ Â...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
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
 
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
 

Algorithms Lecture 7: Graph Algorithms

  • 1. Analysis and Design of Algorithms Graph Algorithms
  • 2. Analysis and Design of Algorithms Graph Directed vs Undirected Graph Acyclic vs Cyclic Graph Backedge Search vs Traversal Breadth First Traversal Depth First Traversal Detect Cycle in a Directed Graph
  • 3. Analysis and Design of Algorithms  Graph data structure consists of a finite set of vertices or nodes. The interconnected objects are represented by points termed as vertices, and the links that connect the vertices are called edges. A B C D E F Node or Vertices Edge
  • 4. Analysis and Design of Algorithms  Vertex: Each node of the graph is represented as a vertex.  V={A,B,C,D,E,F}  Edge: Edge represents a path between two vertices or a line between two vertices.  E={AB,AC,BD,BE,CE,DE,DF,EF} A B C D E F Node or Vertices Edge
  • 5. Analysis and Design of Algorithms A B C A B C Directed Graph Undirected Graph
  • 6. Analysis and Design of Algorithms A B C Acyclic Graph Cyclic Graph A B C
  • 7. Analysis and Design of Algorithms Backedge is an edge that is from node to itself (selfloop), or one to its ancestor. A B A
  • 8. Analysis and Design of Algorithms Search: Look for a given node Traversal: Always visit all nodes
  • 9. Analysis and Design of Algorithms Breadth First Traversal
  • 10. Analysis and Design of Algorithms Breadth First Traversal (BFT) algorithm traverses all nodes on graph in a breadthward motion and uses a queue to remember to get the next vertex.
  • 11. Analysis and Design of Algorithms Layers A B C D E F Layer1 Layer2 Layer3 Layer4
  • 12. Analysis and Design of Algorithms  Visited=  Queue=  Print: A B C D E F 0 0 0 0 0 0 A B C D E F
  • 13. Analysis and Design of Algorithms  Visited=  Queue=  Print: A B C D E F 1 0 0 0 0 0 A B C D E F
  • 14. Analysis and Design of Algorithms  Visited=  Queue= A  Print: A B C D E F 1 0 0 0 0 0 A B C D E F
  • 15. Analysis and Design of Algorithms  Visited=  Queue=  Print: A A B C D E F 1 0 0 0 0 0 A B C D E F
  • 16. Analysis and Design of Algorithms  Visited=  Queue=  Print: A A B C D E F 1 0 0 0 0 0 A B C D E F
  • 17. Analysis and Design of Algorithms  Visited=  Queue=  Print: A A B C D E F 1 0 0 0 0 0 A B C D E F
  • 18. Analysis and Design of Algorithms  Visited=  Queue=  Print: A A B C D E F 1 1 1 0 0 0 A B C D E F
  • 19. Analysis and Design of Algorithms  Visited=  Queue= B C  Print: A A B C D E F 1 1 1 0 0 0 A B C D E F
  • 20. Analysis and Design of Algorithms  Visited=  Queue= C  Print: A B A B C D E F 1 1 1 0 0 0 A B C D E F
  • 21. Analysis and Design of Algorithms  Visited=  Queue= C  Print: A B A B C D E F 1 1 1 0 0 0 A B C D E F
  • 22. Analysis and Design of Algorithms  Visited=  Queue= C  Print: A B A B C D E F 1 1 1 0 0 0 A B C D E F
  • 23. Analysis and Design of Algorithms  Visited=  Queue= C  Print: A B A B C D E F 1 1 1 1 1 0 A B C D E F
  • 24. Analysis and Design of Algorithms  Visited=  Queue= C D E  Print: A B A B C D E F 1 1 1 1 1 0 A B C D E F
  • 25. Analysis and Design of Algorithms  Visited=  Queue= D E  Print: A B C A B C D E F 1 1 1 1 1 0 A B C D E F
  • 26. Analysis and Design of Algorithms  Visited=  Queue= D E  Print: A B C A B C D E F 1 1 1 1 1 0 A B C D E F
  • 27. Analysis and Design of Algorithms  Visited=  Queue= E  Print: A B C A B C D E F 1 1 1 1 1 0 A B C D E F
  • 28. Analysis and Design of Algorithms  Visited=  Queue= E  Print: A B C D A B C D E F 1 1 1 1 1 0 A B C D E F
  • 29. Analysis and Design of Algorithms  Visited=  Queue= E  Print: A B C D A B C D E F 1 1 1 1 1 0 A B C D E F
  • 30. Analysis and Design of Algorithms  Visited=  Queue= E  Print: A B C D A B C D E F 1 1 1 1 1 1 A B C D E F
  • 31. Analysis and Design of Algorithms  Visited=  Queue= E F  Print: A B C D A B C D E F 1 1 1 1 1 1 A B C D E F
  • 32. Analysis and Design of Algorithms  Visited=  Queue= F  Print: A B C D E A B C D E F 1 1 1 1 1 1 A B C D E F
  • 33. Analysis and Design of Algorithms  Visited=  Queue= F  Print: A B C D E A B C D E F 1 1 1 1 1 1 A B C D E F
  • 34. Analysis and Design of Algorithms  Visited=  Queue= F  Print: A B C D E A B C D E F 1 1 1 1 1 1 A B C D E F
  • 35. Analysis and Design of Algorithms  Visited=  Queue=  Print: A B C D E A B C D E F 1 1 1 1 1 1 A B C D E F
  • 36. Analysis and Design of Algorithms  Visited=  Queue=  Print: A B C D E F A B C D E F 1 1 1 1 1 1 A B C D E F
  • 37. Analysis and Design of Algorithms Breadth First on Tree
  • 38. Analysis and Design of Algorithms
  • 39. Analysis and Design of Algorithms  V={0,1,2,3,4,5}  E={01,02,13,14,24,34,35,45} 0 1 2 3 4 5
  • 40. Analysis and Design of Algorithms
  • 41. Analysis and Design of Algorithms  Another Example:
  • 42. Analysis and Design of Algorithms Depth First Traversal
  • 43. Analysis and Design of Algorithms Depth First Traversal (DFT) algorithm traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search.
  • 44. Analysis and Design of Algorithms depthward motion A B C D E F
  • 45. Analysis and Design of Algorithms  Visited= Stack=  Print: A B C D E F 0 0 0 0 0 0 A B C D E F
  • 46. Analysis and Design of Algorithms  Visited= Stack=  Print: A B C D E F 0 0 0 0 0 0 A B C D E F
  • 47. Analysis and Design of Algorithms  Visited= Stack=  Print: A B C D E F 1 0 0 0 0 0 A B C D E F
  • 48. Analysis and Design of Algorithms  Visited= Stack=  Print: A B C D E F 1 0 0 0 0 0 A B C D E F A
  • 49. Analysis and Design of Algorithms  Visited= Stack=  Print: A A B C D E F 1 0 0 0 0 0 A B C D E F A
  • 50. Analysis and Design of Algorithms  Visited= Stack=  Print: A A B C D E F 1 0 0 0 0 0 A B C D E F A
  • 51. Analysis and Design of Algorithms  Visited= Stack=  Print: A A B C D E F 1 1 0 0 0 0 A B C D E F A
  • 52. Analysis and Design of Algorithms  Visited= Stack=  Print: A A B C D E F 1 1 0 0 0 0 A B C D E F B A
  • 53. Analysis and Design of Algorithms  Visited= Stack=  Print: A B A B C D E F 1 1 0 0 0 0 A B C D E F B A
  • 54. Analysis and Design of Algorithms  Visited= Stack=  Print: A B A B C D E F 1 1 0 0 0 0 A B C D E F B A
  • 55. Analysis and Design of Algorithms  Visited= Stack=  Print: A B A B C D E F 1 1 0 1 0 0 A B C D E F B A
  • 56. Analysis and Design of Algorithms  Visited= Stack=  Print: A B A B C D E F 1 1 0 1 0 0 A B C D E F D B A
  • 57. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D A B C D E F 1 1 0 1 0 0 A B C D E F D B A
  • 58. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D A B C D E F 1 1 0 1 0 0 A B C D E F D B A
  • 59. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D A B C D E F 1 1 0 1 0 0 A B C D E F D B A
  • 60. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D A B C D E F 1 1 0 1 1 0 A B C D E F D B A
  • 61. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D A B C D E F 1 1 0 1 1 0 A B C D E F E D B A
  • 62. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E A B C D E F 1 1 0 1 1 0 A B C D E F E D B A
  • 63. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E A B C D E F 1 1 0 1 1 0 A B C D E F E D B A
  • 64. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E A B C D E F 1 1 0 1 1 0 A B C D E F E D B A
  • 65. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E A B C D E F 1 1 0 1 1 1 A B C D E F E D B A
  • 66. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E A B C D E F 1 1 0 1 1 1 A B C D E F F E D B A
  • 67. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F A B C D E F 1 1 0 1 1 1 A B C D E F F E D B A
  • 68. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F A B C D E F 1 1 0 1 1 1 A B C D E F E D B A
  • 69. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F A B C D E F 1 1 0 1 1 1 A B C D E F E D B A
  • 70. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F A B C D E F 1 1 0 1 1 1 A B C D E F E D B A
  • 71. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F A B C D E F 1 1 1 1 1 1 A B C D E F E D B A
  • 72. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F C A B C D E F 1 1 1 1 1 1 A B C D E F C E D B A
  • 73. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F C A B C D E F 1 1 1 1 1 1 A B C D E F E D B A
  • 74. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F C A B C D E F 1 1 1 1 1 1 A B C D E F E D B A
  • 75. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F C A B C D E F 1 1 1 1 1 1 A B C D E F D B A
  • 76. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F C A B C D E F 1 1 1 1 1 1 A B C D E F D B A
  • 77. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F C A B C D E F 1 1 1 1 1 1 A B C D E F B A
  • 78. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F C A B C D E F 1 1 1 1 1 1 A B C D E F A
  • 79. Analysis and Design of Algorithms  Visited= Stack=  Print: A B D E F C A B C D E F 1 1 1 1 1 1 A B C D E F
  • 80. Analysis and Design of Algorithms Depth First on tree
  • 81. Analysis and Design of Algorithms
  • 82. Analysis and Design of Algorithms  V={0,1,2,3,4,5}  E={01,02,13,14,24,34,35,45} 0 1 2 3 4 5
  • 83. Analysis and Design of Algorithms
  • 84. Analysis and Design of Algorithms  Another Example:
  • 85. Analysis and Design of Algorithms Given a directed graph, check whether the graph contains a cycle or not. Your function should return true if the given graph contains at least one cycle, else return false.
  • 86. Analysis and Design of Algorithms Depth First Traversal can be used to detect cycle in a Graph. There is a cycle in a graph only if there is a back edge present in the graph.
  • 87. Analysis and Design of Algorithms  Visited= Stack= A C B D 0 0 0 0 A B C D
  • 88. Analysis and Design of Algorithms  Visited= Stack= A C B D 0 0 0 0 A B C D
  • 89. Analysis and Design of Algorithms  Visited= Stack= A C B D 1 0 0 0 A B C D
  • 90. Analysis and Design of Algorithms  Visited= Stack= A C B D 1 0 0 0 A B C D A
  • 91. Analysis and Design of Algorithms  Visited= Stack= A C B D 1 0 0 0 A B C D A
  • 92. Analysis and Design of Algorithms  Visited= Stack= A C B D 1 1 0 0 A B C D A
  • 93. Analysis and Design of Algorithms  Visited= Stack= A C B D 1 1 0 0 A B C D B A
  • 94. Analysis and Design of Algorithms  Visited= Stack= A C B D 1 1 0 0 A B C D B A
  • 95. Analysis and Design of Algorithms  Visited= Stack= A C B D 1 1 1 0 A B C D B A
  • 96. Analysis and Design of Algorithms  Visited= Stack= A C B D 1 1 1 0 A B C D C B A
  • 97. Analysis and Design of Algorithms  Visited= Stack= A C B D 1 1 1 0 A B C D C B A
  • 98. Analysis and Design of Algorithms  Visited= Stack= A C B D 1 1 1 0 A B C D C B A
  • 99. Analysis and Design of Algorithms Graph contains cycle A C B D
  • 100. Analysis and Design of Algorithms
  • 101. Analysis and Design of Algorithms
  • 102. Analysis and Design of Algorithms facebook.com/mloey mohamedloey@gmail.com twitter.com/mloey linkedin.com/in/mloey mloey@fci.bu.edu.eg mloey.github.io
  • 103. Analysis and Design of Algorithms www.YourCompany.com © 2020 Companyname PowerPoint Business Theme. All Rights Reserved. THANKS FOR YOUR TIME