SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Graph Algorithms
Friday, March 27, 20092
Basics
 A graph G is a pair (V, E), where
 V is a finite set called vertex set of G, and its elements are called vertices
V={v1, v2, v3}
 E is a binary relation on V, i.e. It is called the edge set
of G, and its elements are called edges
E= {(v1, v1), (v1, v2), (v1, v3),
(v2, v1), (v2, v2), (v2, v3),
(v3, v1), (v3, v2), (v3, v3)}
Friday, March 27, 2009
3
Lecture #11
Adapted from slides by Dr A. Sattar
Directed Graphs
 A graph G=(V,E) with vertex set
V={v1, v2 , v3,……vn } is called
directed or digraph if
(vi, vj) ≠ (vj, vi) for i ≠ j
 In other words edges (vi, vj) and
(vj, vi), associated with any pair of
vertices vi, vj are considered
distinct
 Self loops (edges from a vertex to
itself) are possible
 For an edge (u, v), we say it is
incident from or leaves vertex u,
and is incident to or enters vertex v
Friday, March 27, 2009
4
Lecture #11
Adapted from slides by Dr A. Sattar
Undirected Graphs
 In an undirected graph G=(V,E),
the edge set E consists of
unordered pairs of vertices rather
than ordered pairs i.e. (vi, vj) = (vj,
vi) for i ≠ j
 In an undirected graph, self loops
are forbidden and so every edge
consists of two distinct vertices
 For an edge (u, v), we say it is
incident on vertices u and v
Friday, March 27, 20095
Lecture #11
Adapted from slides by Dr A. Sattar
Adjacency
 If (u, v) is an edge in a graph G=(V, E), we say vertex v is adjacent to
vertex u
 When graph is undirected, the adjacency relationship is symmetric
 When graph is directed, the adjacency relationship is not necessarily
symmetric
 If v is adjacent to u in a directed graph, we say u → v
Friday, March 27, 20096
Lecture #11
Adapted from slides by Dr A. Sattar
Complete Graphs
 A graph in which every pair of vertices is adjacent is called a complete
graph. In other words, a graph which has all the edges is referred to as
complete
 An undirected complete graph , with n vertices, has n(n-1)/2 edges. Its
space complexity is O(n2
) . Such a graph is called dense.
 A complete directed graph with n vertices has n2
edges, including edges
associated with all individual vertices. If individual edges are excluded,
the total number of edges is n(n-1)
Friday, March 27, 20097
Lecture #11
Adapted from slides by Dr A. Sattar
Degree
 In an undirected graph, the number of edges incident on a vertex is
called the degree of a vertex
 A vertex with degree 0 is said to be isolated
 In a directed graph, the out-degree of a vertex is the number of edges
leaving it
 In a directed graph, the in-degree of a vertex is the number of edges
entering it
 In a directed graph, the degree of a vertex is the in-degree + the out-
degree
Friday, March 27, 2009
8
Lecture #11
Adapted from slides by Dr A. Sattar
Path
 A path is a sequence of vertices which are connected by consecutive edges
 The number of edges in a path is called the length of the path
 In the figure, path <v1, v2, v4, v6> has length 3
 The path contains the vertices v1, v2, v3, and v4, and edges (v1, v2), (v2, v3),
(v3, v4)
 If there is a path p from u to v, we say v is reachable from u via p
 A path is simple if all vertices in the path are distinct
Friday, March 27, 2009
9
Cycles
 In a directed graph, a path < v0,
v1, …. vk> forms a cycle if v0=vk
and the path contains at least one
edge
 In an undirected graph, a path <
v0, v1, …. vk> forms a cycle if
v0=vk and k>=3
 A cycle is simple if <v1, …. vk>
are distinct
 A self loop is a cycle of length 1
 A graph with no cycles is acyclic
Friday, March 27, 2009
10
Lecture #11
Adapted from slides by Dr A. Sattar
Connected Graphs
 A undirected graph is said to be connected if there exists a path between all
pairs of vertices. In other words, each vertex is reachable from any other vertex
in the graph.
 Connectedness in an undirected graph is simple because the edges can be
traversed in either direction to reach a vertex.
 A directed graph is strongly connected if every two vertices are reachable from
each other
 A graph can have more than one connected components
 An undirected graph is connected if it has exactly one connected component
Friday, March 27, 2009
11
Lecture #11
Adapted from slides by Dr A. Sattar
Weighted Graphs
 A graph in which values w1, w2, w3,……are associated with edges is
called weighted graph. Weights are typically costs or distances in
different applications of graphs.
Friday, March 27, 200912
Lecture #11
Adapted from slides by Dr A. Sattar
Graph Representation
 Two standard ways to represent directed and undirected graphs
 Adjacency list
 Adjacency matrix
 The list representation consists of an array of linked lists, each corresponding to a
vertex.
 The list stores all of the vertices that are linked to a given vertex.
 Let G=(V,E) be a graph. For each u V, the adjacency list Adj(u) contains all the
vertices v such that there is an edge (u ,v ) E
 Vertices in an adjacency list are typically stored in an arbitrary order
Friday, March 27, 200913
Lecture #11
Adapted from slides by Dr A. Sattar
Adjacency List Representation
 If G is a directed graph, the sum of the lengths of all adjacency lists is |E|
 If G is an undirected graph, the sum of the lengths of all adjacency lists is
2|E|
Friday, March 27, 2009
14
Lecture #11
Adapted from slides by Dr A. Sattar
Friday, March 27, 2009
15
Lecture #11
Adapted from slides by Dr A. Sattar
Friday, March 27, 200916
Lecture #11
Adapted from slides by Dr A. Sattar
Advantages and Disadvantages
 Usually preferred because it provides a compact way to represent
sparse graphs – those for which |E| is much less that |V|2
 For both directed and undirected graphs, the amount of
representation required is θ(V+E)
 Can be readily adapted to represent weighted graphs. The weight
is simply stored with vertex v in u’s adjacency list
 There is no quicker way to determine if a given edge (u, v) is
present in the graph than to search for v in the adjacency list
Adj[u]
Friday, March 27, 2009
17
Lecture #11
Adapted from slides by Dr A. Sattar
Adjacency Matrix Representation
 One of the standard ways was of representing graph is to use a matrix to denote
links between pairs of vertices in a graph. The matrix is known as adjacency
matrix
 Let G=(V,E) be a graph, with V={v1,v2,……vn}, and E={(v1,v2),(v1,v3)….}
where n=|V|.
 The adjacency matrix A=(aij) can be represented as
 The size | V|x|V| of adjacency matrix is independent of the number of vertices.
The memory requirement is θ(|V|2
)
Friday, March 27, 200918
Lecture #11
Adapted from slides by Dr A. Sattar
Adjacency Matrix Representation
Friday, March 27, 200919
Lecture #11
Adapted from slides by Dr A. Sattar
Advantages and Disadvantages
 Preferred when the graph is dense – |E| is close to |V|2
 We can tell quickly if there is an edge connecting two given vertices
 Can be readily adapted to represent weighted graphs. The weight is
simply stored as the entry in row u and column v
 The simplicity of an adjacency matrix makes it preferable when graphs
are reasonably small. Also only one bit may be used per entry
Friday, March 27, 200920
Lecture #11
Adapted from slides by Dr A. Sattar
Advantages and Disadvantages
 The in-degree and out-degree of a vertex can be computed by simply
summing the corresponding column and row of the adjacency matrix.
In-degree is number of edges terminating at, and out-degree the number
of edges originating from, a vertex.
 A major disadvantage of matrix representation is inefficient utilization
of storage space if the graph is not dense, that is, there are fewer edges.
For example, the adjacency matrix of a graph with 10 vertices and four
edges would contain 100 elements, only four 1’s. The 96% storage
space would be wasted
Friday, March 27, 200921
Lecture #11
Adapted from slides by Dr A. Sattar
Trees
 Free Trees
 A free tree is a connected, acyclic, undirected graph
 We often omit ‘free’ when we say that a graph is a tree
 If an undirected graph is acyclic but possibly disconnected, it is a
forest
Friday, March 27, 200922
Lecture #11
Adapted from slides by Dr A. Sattar
Properties of Free Trees
 Let G=(V,E) be an undirected graph. The following statements
are equivalent:
 G is a free tree
 Any two vertices in G are connected by a unique simple path
 G is connected, but if any edge is removed from E, the resulting
graph is disconnected
 G is connected and |E|=|V|-1
 G is acyclic and |E|=|V|-1
 G is acyclic, but if any edge is added to E, the resulting graph
contains a cycle
Friday, March 27, 200923
Lecture #11
Adapted from slides by Dr A. Sattar
Rooted Trees
 A rooted tree is a free tree in which one of the vertices is
distinguished from the others
 This distinguished vertex is called the root of the tree
 Consider a node x in rooted tree T with root r. Any node y on the
unique path from r to x is called an ancestor of x
 x is a descendant of y

Weitere ähnliche Inhalte

Was ist angesagt?

Graph in data structure
Graph in data structureGraph in data structure
Graph in data structureAbrish06
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 
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 algorithmGaurav Kolekar
 
Max flow min cut
Max flow min cutMax flow min cut
Max flow min cutMayank Garg
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning treeSTEFFY D
 
Graph representation
Graph representationGraph representation
Graph representationTech_MX
 
DATA STRUCTURES unit 4.pptx
DATA STRUCTURES unit 4.pptxDATA STRUCTURES unit 4.pptx
DATA STRUCTURES unit 4.pptxShivamKrPathak
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]DEEPIKA T
 
PRIM’S AND KRUSKAL’S ALGORITHM
PRIM’S AND KRUSKAL’S  ALGORITHMPRIM’S AND KRUSKAL’S  ALGORITHM
PRIM’S AND KRUSKAL’S ALGORITHMJaydeepDesai10
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithmsana younas
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraRoshan Tailor
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's AlgorithmTanmay Baranwal
 

Was ist angesagt? (20)

Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
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
 
Trees and graphs
Trees and graphsTrees and graphs
Trees and graphs
 
Max flow min cut
Max flow min cutMax flow min cut
Max flow min cut
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Graph representation
Graph representationGraph representation
Graph representation
 
DATA STRUCTURES unit 4.pptx
DATA STRUCTURES unit 4.pptxDATA STRUCTURES unit 4.pptx
DATA STRUCTURES unit 4.pptx
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
PRIM’S AND KRUSKAL’S ALGORITHM
PRIM’S AND KRUSKAL’S  ALGORITHMPRIM’S AND KRUSKAL’S  ALGORITHM
PRIM’S AND KRUSKAL’S ALGORITHM
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
Trees
TreesTrees
Trees
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Hash tables
Hash tablesHash tables
Hash tables
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
 

Ähnlich wie Graph algorithms

Chapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptChapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptishan743441
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxasimshahzad8611
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptxARVIND SARDAR
 
1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptx1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptxswapnilbs2728
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabatinabati
 
Tn 110 lecture 8
Tn 110 lecture 8Tn 110 lecture 8
Tn 110 lecture 8ITNet
 
unit5 graphs (DS).pptx
unit5 graphs (DS).pptxunit5 graphs (DS).pptx
unit5 graphs (DS).pptxSri Latha
 
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
 
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-13sumitbardhan
 

Ähnlich wie Graph algorithms (20)

Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
graph.ppt
graph.pptgraph.ppt
graph.ppt
 
Graph
GraphGraph
Graph
 
Chapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptChapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).ppt
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
graph.ppt
graph.pptgraph.ppt
graph.ppt
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
Graph 01
Graph 01Graph 01
Graph 01
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptx1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptx
 
Graphs.pdf
Graphs.pdfGraphs.pdf
Graphs.pdf
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
 
Tn 110 lecture 8
Tn 110 lecture 8Tn 110 lecture 8
Tn 110 lecture 8
 
Graph theory presentation
Graph theory presentationGraph theory presentation
Graph theory presentation
 
DIGITAL TEXT BOOK
DIGITAL TEXT BOOKDIGITAL TEXT BOOK
DIGITAL TEXT BOOK
 
unit5 graphs (DS).pptx
unit5 graphs (DS).pptxunit5 graphs (DS).pptx
unit5 graphs (DS).pptx
 
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
 
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
 

Mehr von University of Haripur (8)

Linux firewalls comparison 5 minute Presentation
Linux firewalls comparison 5 minute PresentationLinux firewalls comparison 5 minute Presentation
Linux firewalls comparison 5 minute Presentation
 
Windows 7, 8, 8.1 & 10
Windows 7, 8, 8.1 & 10Windows 7, 8, 8.1 & 10
Windows 7, 8, 8.1 & 10
 
Conductors and its types
Conductors and its typesConductors and its types
Conductors and its types
 
Requirement Engineering
Requirement EngineeringRequirement Engineering
Requirement Engineering
 
Software Requiremnets
Software RequiremnetsSoftware Requiremnets
Software Requiremnets
 
Unified process Model
Unified process ModelUnified process Model
Unified process Model
 
Galobal Warming
Galobal WarmingGalobal Warming
Galobal Warming
 
Recycling of wastes
Recycling of wastesRecycling of wastes
Recycling of wastes
 

Kürzlich hochgeladen

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
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 17Celine George
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.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
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
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
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
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-701bronxfugly43
 
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.pptxJisc
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
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 PractiseAnaAcapella
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
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...Poonam Aher Patil
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
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
 
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
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 

Kürzlich hochgeladen (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
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
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
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
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
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
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
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
 
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
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
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...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
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
 
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...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

Graph algorithms

  • 2. Friday, March 27, 20092 Basics  A graph G is a pair (V, E), where  V is a finite set called vertex set of G, and its elements are called vertices V={v1, v2, v3}  E is a binary relation on V, i.e. It is called the edge set of G, and its elements are called edges E= {(v1, v1), (v1, v2), (v1, v3), (v2, v1), (v2, v2), (v2, v3), (v3, v1), (v3, v2), (v3, v3)}
  • 3. Friday, March 27, 2009 3 Lecture #11 Adapted from slides by Dr A. Sattar Directed Graphs  A graph G=(V,E) with vertex set V={v1, v2 , v3,……vn } is called directed or digraph if (vi, vj) ≠ (vj, vi) for i ≠ j  In other words edges (vi, vj) and (vj, vi), associated with any pair of vertices vi, vj are considered distinct  Self loops (edges from a vertex to itself) are possible  For an edge (u, v), we say it is incident from or leaves vertex u, and is incident to or enters vertex v
  • 4. Friday, March 27, 2009 4 Lecture #11 Adapted from slides by Dr A. Sattar Undirected Graphs  In an undirected graph G=(V,E), the edge set E consists of unordered pairs of vertices rather than ordered pairs i.e. (vi, vj) = (vj, vi) for i ≠ j  In an undirected graph, self loops are forbidden and so every edge consists of two distinct vertices  For an edge (u, v), we say it is incident on vertices u and v
  • 5. Friday, March 27, 20095 Lecture #11 Adapted from slides by Dr A. Sattar Adjacency  If (u, v) is an edge in a graph G=(V, E), we say vertex v is adjacent to vertex u  When graph is undirected, the adjacency relationship is symmetric  When graph is directed, the adjacency relationship is not necessarily symmetric  If v is adjacent to u in a directed graph, we say u → v
  • 6. Friday, March 27, 20096 Lecture #11 Adapted from slides by Dr A. Sattar Complete Graphs  A graph in which every pair of vertices is adjacent is called a complete graph. In other words, a graph which has all the edges is referred to as complete  An undirected complete graph , with n vertices, has n(n-1)/2 edges. Its space complexity is O(n2 ) . Such a graph is called dense.  A complete directed graph with n vertices has n2 edges, including edges associated with all individual vertices. If individual edges are excluded, the total number of edges is n(n-1)
  • 7. Friday, March 27, 20097 Lecture #11 Adapted from slides by Dr A. Sattar Degree  In an undirected graph, the number of edges incident on a vertex is called the degree of a vertex  A vertex with degree 0 is said to be isolated  In a directed graph, the out-degree of a vertex is the number of edges leaving it  In a directed graph, the in-degree of a vertex is the number of edges entering it  In a directed graph, the degree of a vertex is the in-degree + the out- degree
  • 8. Friday, March 27, 2009 8 Lecture #11 Adapted from slides by Dr A. Sattar Path  A path is a sequence of vertices which are connected by consecutive edges  The number of edges in a path is called the length of the path  In the figure, path <v1, v2, v4, v6> has length 3  The path contains the vertices v1, v2, v3, and v4, and edges (v1, v2), (v2, v3), (v3, v4)  If there is a path p from u to v, we say v is reachable from u via p  A path is simple if all vertices in the path are distinct
  • 9. Friday, March 27, 2009 9 Cycles  In a directed graph, a path < v0, v1, …. vk> forms a cycle if v0=vk and the path contains at least one edge  In an undirected graph, a path < v0, v1, …. vk> forms a cycle if v0=vk and k>=3  A cycle is simple if <v1, …. vk> are distinct  A self loop is a cycle of length 1  A graph with no cycles is acyclic
  • 10. Friday, March 27, 2009 10 Lecture #11 Adapted from slides by Dr A. Sattar Connected Graphs  A undirected graph is said to be connected if there exists a path between all pairs of vertices. In other words, each vertex is reachable from any other vertex in the graph.  Connectedness in an undirected graph is simple because the edges can be traversed in either direction to reach a vertex.  A directed graph is strongly connected if every two vertices are reachable from each other  A graph can have more than one connected components  An undirected graph is connected if it has exactly one connected component
  • 11. Friday, March 27, 2009 11 Lecture #11 Adapted from slides by Dr A. Sattar Weighted Graphs  A graph in which values w1, w2, w3,……are associated with edges is called weighted graph. Weights are typically costs or distances in different applications of graphs.
  • 12. Friday, March 27, 200912 Lecture #11 Adapted from slides by Dr A. Sattar Graph Representation  Two standard ways to represent directed and undirected graphs  Adjacency list  Adjacency matrix  The list representation consists of an array of linked lists, each corresponding to a vertex.  The list stores all of the vertices that are linked to a given vertex.  Let G=(V,E) be a graph. For each u V, the adjacency list Adj(u) contains all the vertices v such that there is an edge (u ,v ) E  Vertices in an adjacency list are typically stored in an arbitrary order
  • 13. Friday, March 27, 200913 Lecture #11 Adapted from slides by Dr A. Sattar Adjacency List Representation  If G is a directed graph, the sum of the lengths of all adjacency lists is |E|  If G is an undirected graph, the sum of the lengths of all adjacency lists is 2|E|
  • 14. Friday, March 27, 2009 14 Lecture #11 Adapted from slides by Dr A. Sattar
  • 15. Friday, March 27, 2009 15 Lecture #11 Adapted from slides by Dr A. Sattar
  • 16. Friday, March 27, 200916 Lecture #11 Adapted from slides by Dr A. Sattar Advantages and Disadvantages  Usually preferred because it provides a compact way to represent sparse graphs – those for which |E| is much less that |V|2  For both directed and undirected graphs, the amount of representation required is θ(V+E)  Can be readily adapted to represent weighted graphs. The weight is simply stored with vertex v in u’s adjacency list  There is no quicker way to determine if a given edge (u, v) is present in the graph than to search for v in the adjacency list Adj[u]
  • 17. Friday, March 27, 2009 17 Lecture #11 Adapted from slides by Dr A. Sattar Adjacency Matrix Representation  One of the standard ways was of representing graph is to use a matrix to denote links between pairs of vertices in a graph. The matrix is known as adjacency matrix  Let G=(V,E) be a graph, with V={v1,v2,……vn}, and E={(v1,v2),(v1,v3)….} where n=|V|.  The adjacency matrix A=(aij) can be represented as  The size | V|x|V| of adjacency matrix is independent of the number of vertices. The memory requirement is θ(|V|2 )
  • 18. Friday, March 27, 200918 Lecture #11 Adapted from slides by Dr A. Sattar Adjacency Matrix Representation
  • 19. Friday, March 27, 200919 Lecture #11 Adapted from slides by Dr A. Sattar Advantages and Disadvantages  Preferred when the graph is dense – |E| is close to |V|2  We can tell quickly if there is an edge connecting two given vertices  Can be readily adapted to represent weighted graphs. The weight is simply stored as the entry in row u and column v  The simplicity of an adjacency matrix makes it preferable when graphs are reasonably small. Also only one bit may be used per entry
  • 20. Friday, March 27, 200920 Lecture #11 Adapted from slides by Dr A. Sattar Advantages and Disadvantages  The in-degree and out-degree of a vertex can be computed by simply summing the corresponding column and row of the adjacency matrix. In-degree is number of edges terminating at, and out-degree the number of edges originating from, a vertex.  A major disadvantage of matrix representation is inefficient utilization of storage space if the graph is not dense, that is, there are fewer edges. For example, the adjacency matrix of a graph with 10 vertices and four edges would contain 100 elements, only four 1’s. The 96% storage space would be wasted
  • 21. Friday, March 27, 200921 Lecture #11 Adapted from slides by Dr A. Sattar Trees  Free Trees  A free tree is a connected, acyclic, undirected graph  We often omit ‘free’ when we say that a graph is a tree  If an undirected graph is acyclic but possibly disconnected, it is a forest
  • 22. Friday, March 27, 200922 Lecture #11 Adapted from slides by Dr A. Sattar Properties of Free Trees  Let G=(V,E) be an undirected graph. The following statements are equivalent:  G is a free tree  Any two vertices in G are connected by a unique simple path  G is connected, but if any edge is removed from E, the resulting graph is disconnected  G is connected and |E|=|V|-1  G is acyclic and |E|=|V|-1  G is acyclic, but if any edge is added to E, the resulting graph contains a cycle
  • 23. Friday, March 27, 200923 Lecture #11 Adapted from slides by Dr A. Sattar Rooted Trees  A rooted tree is a free tree in which one of the vertices is distinguished from the others  This distinguished vertex is called the root of the tree  Consider a node x in rooted tree T with root r. Any node y on the unique path from r to x is called an ancestor of x  x is a descendant of y