10.graph

jashobhan pradhan
jashobhan pradhanStudent at NIT ROURKELA
Data Structure and
Algorithm (CS-102)
Ashok K Turuk
Graph
• A graph is a collection of nodes (or
vertices, singular is vertex) and edges (or
arcs)
– Each node contains an element
– Each edge connects two nodes together (or
possibly the same node to itself) and may
contain an edge attribute
• A directed graph is one in which the edges
have a direction
• An undirected graph is one in which the
edges do not have a direction
2
What is a Graph?
• A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
• An edge e = (u,v) is a pair of vertices
• Example:
a b
c
d e
V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e), (d,e)}
some problems that can be
represented by a graph
• computer networks
• airline flights
• road map
• course prerequisite structure
• tasks for completing a job
• flow of control through a
program
• many more
a digraph
A
B
C D
E
V = [A, B, C, D, E]
E = [<A,B>, <B,C>, <C,B>, <A,C>, <A,E>, <C,D>]
Graph terminology
• The size of a graph is the number of nodes in it
• The empty graph has size zero (no nodes)
• If two nodes are connected by an edge, they are
neighbors (and the nodes are adjacent to each other)
• The degree of a node is the number of edges it has
• For directed graphs,
– If a directed edge goes from node S to node D, we call S the
source and D the destination of the edge
• The edge is an out-edge of S and an in-edge of D
• S is a predecessor of D, and D is a successor of S
– The in-degree of a node is the number of in-edges it has
– The out-degree of a node is the number of out-edges it has
6
7
Terminology:
Path
• path: sequence of
vertices v1,v2,. . .vk
such that consecutive
vertices vi and vi+1 are
adjacent.
3
3 3
3
2
a b
c
d e
a b
c
d e
a b e d c b e d c
More Terminology
• simple path: no repeated vertices
• cycle: simple path, except that the last vertex is the same
as the first vertex
a b
c
d e
b e c
a c d a
a b
c
d e
Even More Terminology
• subgraph: subset of vertices and edges forming a graph
• connected component: maximal connected subgraph. E.g.,
the graph below has 3 connected components.
connected not connected
•connected graph: any two vertices are connected
by some path
0 0
1 2 3
1 2 0
1 2
3
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G1
0 0
1
0
1
2
0
1
2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G3
0
1 2
3
G1
0
1
2
G3
Subgraphs Examples
More…
• tree - connected graph without cycles
• forest - collection of trees
tree
forest
tree
tree
tree
Connectivity
• Let n = #vertices, and m = #edges
• A complete graph: one in which all pairs of vertices
are adjacent
• How many total edges in a complete graph?
– Each of the n vertices is incident to n-1 edges, however, we
would have counted each edge twice! Therefore,
intuitively, m = n(n -1)/2.
• Therefore, if a graph is not complete, m < n(n -1)/2
n 5
m  (5 
More Connectivity
n = #vertices
m = #edges
• For a tree m = n - 1
n  5
m  4
n  5
m  3
If m < n - 1, G is not
connected
Oriented (Directed) Graph
• A graph where edges are directed
Directed vs. Undirected Graph
• An undirected graph is one in which the
pair of vertices in a edge is unordered,
(v0, v1) = (v1,v0)
• A directed graph is one in which each
edge is a directed pair of vertices, <v0,
v1> != <v1,v0> tail head
Graph terminology
• An undirected graph is connected if there is a
path from every node to every other node
• A directed graph is strongly connected if there is
a path from every node to every other node
• A directed graph is weakly connected if the
underlying undirected graph is connected
• Node X is reachable from node Y if there is a path
from Y to X
• A subset of the nodes of the graph is a connected
component (or just a component) if there is a path
from every node in the subset to every other node
in the subset
16
graph data structures
• storing the vertices
– each vertex has a unique identifier and,
maybe, other information
– for efficiency, associate each vertex with a
number that can be used as an index
• storing the edges
– adjacency matrix – represent all possible
edges
– adjacency lists – represent only the
existing edges
storing the vertices
• when a vertex is added to the graph,
assign it a number
– vertices are numbered between 0 and n-1
• graph operations start by looking up
the number associated with a vertex
• many data structures to use
– any of the associative data structures
– for small graphs a vector can be used
• search will be O(n)
the vertex vector
A
B
C D
E
0
1
2 3
4
0
1
2
3
4
A
B
C
D
E
adjacency matrix
A
B
C D
E
0
1
2 3
4
0
1
2
3
4
A
B
C
D
E
0 1 1 0 1
0 0 1 0 0
0 1 0 1 0
0 0 0 0 0
0 0 0 0 0
0
1
2
3
4
0 1 2 3 4
Examples for Adjacency Matrix
0
1
1
1
1
0
1
1
1
1
0
1
1
1
1
0












0
1
0
1
0
0
0
1
0










G1
G2
0
1 2
3
0
1
2
symmetric
Asymmetric
maximum # edges?
a V2 matrix is needed for
a graph with V vertices
many graphs are “sparse”
• degree of “sparseness” key factor in
choosing a data structure for edges
– adjacency matrix requires space for all
possible edges
– adjacency list requires space for existing
edges only
• affects amount of memory space
needed
• affects efficiency of graph operations
adjacency lists
A
B
C D
E
0
1
2 3
4
0
1
2
3
4
1 2 4
2
1 3
0
1
2
3
4
A
B
C
D
E
0
1
2
3
0
1
2
1 2 3
0 2 3
0 1 3
0 1 2
G1
1
0 2
G3
0
1 2
3
0
1
2
Least-Cost Algorithms
Dijkstra’s Algorithm
Find the least cost path from a given node to all
other nodes in the network
N = Set of nodes in the network
s = Source node
M = Set of nodes so far incorporated by the
algorithm
dij = link cost from node i to node j, dii = 0
dij = ∞ if two nodes are not directly connected
dij >= 0 of two nodes are directly connected
Dn = cost of the least-cost path from node s to
node n that is currently known to the algorithm
1. Initialize
M = {s} [ set of nodes so far
incorporated consists of only the
source node]
Dn = dsn for n != s (initial path costs to
the neighbor nodes are simply the link
cost)
2. Find the neighbor node not in M that
has the least-cost path from node s
and incorporate that node into M.
Find w !Є M such that Dw = min { Dj } for j
!Є M
3. Update the least-cost path:
Dn = min[ Dn , Dw + dwn ] for all n !Є M
If the latter term is minimum, path from
s to n is now the path from s to w,
concatenated with the link from w to n
2 3
4 5
61
6
2
7
1
3 1
8
4
8
2
5
3
2
1
3
5
It M D2 Path D3 Path D4 Path D5 Path D6 Path
1 {1} 2 1 -2 5 1-3 1 1-4 ∞ - ∞ -
2 {1, 4} 2 1-2 4 1-4-3 1 1-4 2 1-4-5 ∞ -
3 {1,2,4} 2 1-2 4 1-4-3 1 1-4 2 1-4-5 ∞ -
It = Iteration
1 de 30

Recomendados

Lecture 14 data structures and algorithms por
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsAakash deep Singhal
1.8K vistas30 diapositivas
Graph por
GraphGraph
GraphDr Sandeep Kumar Poonia
2.5K vistas78 diapositivas
Graph in data structure por
Graph in data structureGraph in data structure
Graph in data structureAbrish06
42.8K vistas24 diapositivas
Data structure - Graph por
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
6.3K vistas28 diapositivas
Graph representation por
Graph representationGraph representation
Graph representationTech_MX
37.2K vistas34 diapositivas
Skiena algorithm 2007 lecture10 graph data strctures por
Skiena algorithm 2007 lecture10 graph data strcturesSkiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strctureszukun
2.2K vistas29 diapositivas

Más contenido relacionado

La actualidad más candente

Graphs In Data Structure por
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
45.6K vistas32 diapositivas
Data Structure : Graph and Graph Traversing por
Data Structure : Graph and Graph TraversingData Structure : Graph and Graph Traversing
Data Structure : Graph and Graph TraversingVikas Chandwani
246 vistas20 diapositivas
Problem Solving with Algorithms and Data Structure - Graphs por
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsYi-Lung Tsai
2.3K vistas68 diapositivas
Graph por
GraphGraph
GraphUsha Mahalingam
149 vistas72 diapositivas
Graph theory por
Graph theoryGraph theory
Graph theoryAparnaKumari31
2K vistas13 diapositivas
Data structure por
Data structureData structure
Data structurejagjot singh chopra
267 vistas17 diapositivas

La actualidad más candente(19)

Graphs In Data Structure por Anuj Modi
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
Anuj Modi45.6K vistas
Data Structure : Graph and Graph Traversing por Vikas Chandwani
Data Structure : Graph and Graph TraversingData Structure : Graph and Graph Traversing
Data Structure : Graph and Graph Traversing
Vikas Chandwani246 vistas
Problem Solving with Algorithms and Data Structure - Graphs por Yi-Lung Tsai
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
Yi-Lung Tsai2.3K vistas
Graph terminologies & special type graphs por Nabeel Ahsen
Graph terminologies & special type graphsGraph terminologies & special type graphs
Graph terminologies & special type graphs
Nabeel Ahsen6.4K vistas
Applications of graphs por Tech_MX
Applications of graphsApplications of graphs
Applications of graphs
Tech_MX20.3K vistas
Graph Theory Introduction por MANISH T I
Graph Theory IntroductionGraph Theory Introduction
Graph Theory Introduction
MANISH T I1.9K vistas
Graph data structure por Tech_MX
Graph data structureGraph data structure
Graph data structure
Tech_MX14.1K vistas
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring por Saurabh Kaushik
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Saurabh Kaushik10.7K vistas
graph theory por ganith2k13
graph theory graph theory
graph theory
ganith2k1314.5K vistas

Similar a 10.graph

Graphs in data structures por
Graphs in data structuresGraphs in data structures
Graphs in data structuresSavit Chandra
1K vistas32 diapositivas
Unit ix graph por
Unit   ix    graph Unit   ix    graph
Unit ix graph Tribhuvan University
988 vistas42 diapositivas
Unit 9 graph por
Unit   9 graphUnit   9 graph
Unit 9 graphDabbal Singh Mahara
222 vistas43 diapositivas
Graph in data structure por
Graph in data structureGraph in data structure
Graph in data structurePooja Bhojwani
196 vistas28 diapositivas
Lecture 5b graphs and hashing por
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
1.2K vistas56 diapositivas
graph ASS (1).ppt por
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).pptARVIND SARDAR
33 vistas158 diapositivas

Similar a 10.graph(20)

Lecture 5b graphs and hashing por Victor Palmar
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
Victor Palmar1.2K vistas
358 33 powerpoint-slides_13-graphs_chapter-13 por sumitbardhan
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
sumitbardhan3.9K vistas
graph.pptx por hijigaf
graph.pptxgraph.pptx
graph.pptx
hijigaf14 vistas
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf por ChristianKapsales1
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Social network analysis por Stefanie Zhao
Social network analysisSocial network analysis
Social network analysis
Stefanie Zhao802 vistas

10.graph

  • 1. Data Structure and Algorithm (CS-102) Ashok K Turuk
  • 2. Graph • A graph is a collection of nodes (or vertices, singular is vertex) and edges (or arcs) – Each node contains an element – Each edge connects two nodes together (or possibly the same node to itself) and may contain an edge attribute • A directed graph is one in which the edges have a direction • An undirected graph is one in which the edges do not have a direction 2
  • 3. What is a Graph? • A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V • An edge e = (u,v) is a pair of vertices • Example: a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)}
  • 4. some problems that can be represented by a graph • computer networks • airline flights • road map • course prerequisite structure • tasks for completing a job • flow of control through a program • many more
  • 5. a digraph A B C D E V = [A, B, C, D, E] E = [<A,B>, <B,C>, <C,B>, <A,C>, <A,E>, <C,D>]
  • 6. Graph terminology • The size of a graph is the number of nodes in it • The empty graph has size zero (no nodes) • If two nodes are connected by an edge, they are neighbors (and the nodes are adjacent to each other) • The degree of a node is the number of edges it has • For directed graphs, – If a directed edge goes from node S to node D, we call S the source and D the destination of the edge • The edge is an out-edge of S and an in-edge of D • S is a predecessor of D, and D is a successor of S – The in-degree of a node is the number of in-edges it has – The out-degree of a node is the number of out-edges it has 6
  • 7. 7 Terminology: Path • path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent. 3 3 3 3 2 a b c d e a b c d e a b e d c b e d c
  • 8. More Terminology • simple path: no repeated vertices • cycle: simple path, except that the last vertex is the same as the first vertex a b c d e b e c a c d a a b c d e
  • 9. Even More Terminology • subgraph: subset of vertices and edges forming a graph • connected component: maximal connected subgraph. E.g., the graph below has 3 connected components. connected not connected •connected graph: any two vertices are connected by some path
  • 10. 0 0 1 2 3 1 2 0 1 2 3 (i) (ii) (iii) (iv) (a) Some of the subgraph of G1 0 0 1 0 1 2 0 1 2 (i) (ii) (iii) (iv) (b) Some of the subgraph of G3 0 1 2 3 G1 0 1 2 G3 Subgraphs Examples
  • 11. More… • tree - connected graph without cycles • forest - collection of trees tree forest tree tree tree
  • 12. Connectivity • Let n = #vertices, and m = #edges • A complete graph: one in which all pairs of vertices are adjacent • How many total edges in a complete graph? – Each of the n vertices is incident to n-1 edges, however, we would have counted each edge twice! Therefore, intuitively, m = n(n -1)/2. • Therefore, if a graph is not complete, m < n(n -1)/2 n 5 m  (5 
  • 13. More Connectivity n = #vertices m = #edges • For a tree m = n - 1 n  5 m  4 n  5 m  3 If m < n - 1, G is not connected
  • 14. Oriented (Directed) Graph • A graph where edges are directed
  • 15. Directed vs. Undirected Graph • An undirected graph is one in which the pair of vertices in a edge is unordered, (v0, v1) = (v1,v0) • A directed graph is one in which each edge is a directed pair of vertices, <v0, v1> != <v1,v0> tail head
  • 16. Graph terminology • An undirected graph is connected if there is a path from every node to every other node • A directed graph is strongly connected if there is a path from every node to every other node • A directed graph is weakly connected if the underlying undirected graph is connected • Node X is reachable from node Y if there is a path from Y to X • A subset of the nodes of the graph is a connected component (or just a component) if there is a path from every node in the subset to every other node in the subset 16
  • 17. graph data structures • storing the vertices – each vertex has a unique identifier and, maybe, other information – for efficiency, associate each vertex with a number that can be used as an index • storing the edges – adjacency matrix – represent all possible edges – adjacency lists – represent only the existing edges
  • 18. storing the vertices • when a vertex is added to the graph, assign it a number – vertices are numbered between 0 and n-1 • graph operations start by looking up the number associated with a vertex • many data structures to use – any of the associative data structures – for small graphs a vector can be used • search will be O(n)
  • 19. the vertex vector A B C D E 0 1 2 3 4 0 1 2 3 4 A B C D E
  • 20. adjacency matrix A B C D E 0 1 2 3 4 0 1 2 3 4 A B C D E 0 1 1 0 1 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 0 1 2 3 4
  • 21. Examples for Adjacency Matrix 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0             0 1 0 1 0 0 0 1 0           G1 G2 0 1 2 3 0 1 2 symmetric Asymmetric
  • 22. maximum # edges? a V2 matrix is needed for a graph with V vertices
  • 23. many graphs are “sparse” • degree of “sparseness” key factor in choosing a data structure for edges – adjacency matrix requires space for all possible edges – adjacency list requires space for existing edges only • affects amount of memory space needed • affects efficiency of graph operations
  • 24. adjacency lists A B C D E 0 1 2 3 4 0 1 2 3 4 1 2 4 2 1 3 0 1 2 3 4 A B C D E
  • 25. 0 1 2 3 0 1 2 1 2 3 0 2 3 0 1 3 0 1 2 G1 1 0 2 G3 0 1 2 3 0 1 2
  • 26. Least-Cost Algorithms Dijkstra’s Algorithm Find the least cost path from a given node to all other nodes in the network N = Set of nodes in the network s = Source node M = Set of nodes so far incorporated by the algorithm dij = link cost from node i to node j, dii = 0 dij = ∞ if two nodes are not directly connected dij >= 0 of two nodes are directly connected Dn = cost of the least-cost path from node s to node n that is currently known to the algorithm
  • 27. 1. Initialize M = {s} [ set of nodes so far incorporated consists of only the source node] Dn = dsn for n != s (initial path costs to the neighbor nodes are simply the link cost) 2. Find the neighbor node not in M that has the least-cost path from node s and incorporate that node into M.
  • 28. Find w !Є M such that Dw = min { Dj } for j !Є M 3. Update the least-cost path: Dn = min[ Dn , Dw + dwn ] for all n !Є M If the latter term is minimum, path from s to n is now the path from s to w, concatenated with the link from w to n
  • 29. 2 3 4 5 61 6 2 7 1 3 1 8 4 8 2 5 3 2 1 3 5
  • 30. It M D2 Path D3 Path D4 Path D5 Path D6 Path 1 {1} 2 1 -2 5 1-3 1 1-4 ∞ - ∞ - 2 {1, 4} 2 1-2 4 1-4-3 1 1-4 2 1-4-5 ∞ - 3 {1,2,4} 2 1-2 4 1-4-3 1 1-4 2 1-4-5 ∞ - It = Iteration