SlideShare a Scribd company logo
Searching in a Graph
Representations of Graphs
•Adjacency Matrices
•Adjacency Lists
Adjacency Matrices
Graphs G = (V, E) can be represented by adjacency matrices
G[v1..v|V |, v1..v|V |], where the rows and columns are indexed by the nodes,
and the entries G[vi, vj] represent the edges. In the case of unlabeled
graphs, the entries are just boolean values.
A B C D
A 0 1 1 1
B 1 0 0 1
C 1 0 0 1
D 1 1 1 0
Adjacency Matrices
In case of labeled graphs, the labels themselves may be introduced into
the entries.
A B C D
A 10 4 1
B 15
C 9
D
Adjacency matrices require O(|V |2) space, and so they are space-efficient only when
they are dense (that is, when the graphs have many edges). Time-wise, the adjacency
matrices allow easy addition and deletion of edges.
Adjacency Lists
A representation of the graph consisting of a list of nodes, with each
node containing a list of its neighboring nodes.
This representation takes O(|V | + |E|) space.
Graph Traversal
•Depth-First Traversal
•Breadth-First Traversal
Depth-First Traversal
• algorithm dft(x)
visit(x)
FOR each y such that (x,y) is an edge DO
IF <y was not visited yet >
THEN dft(y)
Depth-First Traversal
• A recursive algorithm implicitly recording a
“backtracking” path from the root to the
node currently under consideration
Depth-First Traversal
Depth-First Traversal
Depth-First Traversal
Depth-First Traversal
Depth-First Traversal
•Depth first search is another way of traversing graphs,
which is closely related to preorder traversal of a tree.
•the Breath-first search tree is typically "short and
bushy", the DFS tree is typically "long and stringy".
Breadth-First Traversal
Visit the nodes at level i before the nodes of level i+1.
Breadth-First Traversal
visit(start node)
queue <- start node
WHILE queue is not empty DO
x <- queue
FOR each y such that (x,y) is an edge
and y has not been visited yet DO
visit(y)
queue <- y
END
END
Breadth-First Traversal
Breadth-First Traversal
Breadth-First Traversal
Breadth-First Traversal
• Each vertex is clearly
• marked at most once,
• added to the list at most once (since that happens only when it's
marked), and
• removed from the list at most once.
Since the time to process a vertex is proportional to the length of its
adjacency list, the total time for the whole algorithm is O(m).
• A tree T constructed by the algorithm is called a breadth first search
tree.
• The traversal goes a level at a time, left to right within a level (where a
level is defined simply in terms of distance from the root of the tree).
Breadth-First Traversal
• Every edge of G can be classified into one of three groups.
• Some edges are in T themselves.
• Some connect two vertices at the same level of T.
• The remaining ones connect two vertices on two adjacent levels. It
is not possible for an edge to skip a level.
• Breadth-first search tree really is a shortest path tree starting from
its root.
Relation between BFS and DFS
bfs(G)
{
list L = empty tree
T = empty
choose a starting vertex x
search(x)
while(L nonempty)
remove edge (v,w) from start of L
if w not yet visited
{
add (v,w) to T
search(w)
}
}
dfs(G)
{
list L = empty
tree T = empty
choose a starting vertex x
search(x)
while(L nonempty)
remove edge (v,w) from end of L
if w not yet visited
{
add (v,w) to T
search(w)
}
}
search(vertex v) {
visit(v);
for each edge (v,w)
add edge (v,w) to end of L
}
Relation between BFS and DFS
Both of these search algorithms now keep a list of edges to
explore; the only difference between the two is
while both algorithms adds items to the end of L,
•BFS removes them from the beginning, which results in
maintaining the list as a queue
•DFS removes them from the end, maintaining the list as a
stack.
BFS and DFS in directed graphs
The same search routines work essentially unmodified for
directed graphs.
The only difference is that when exploring a vertex v, we
only want to look at edges (v,w) going out of v; we ignore
the other edges coming into v.
For BFS in directed graphs each edge of the graph either
•connects two vertices at the same level
•goes down exactly one level
•goes up any number of levels.
For DFS, each edge either connects an ancestor to
•a descendant
•a descendant to an ancestor
•one node to a node in a previously visited subtree.
Searching
Searching
Searching
Searching
Searching
Searching
Searching
Searching
Searching
End
End

More Related Content

Similar to 8-Graph.ppt

LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
Graphss
GraphssGraphss
Graphss
fika sweety
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
Dabbal Singh Mahara
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
Victor Palmar
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
Hanif Durad
 
Graps 2
Graps 2Graps 2
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdf
amitbhachne
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
Keno benti
 
Graphs
GraphsGraphs
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
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
ChristianKapsales1
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
SwarndeviKm
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
Amrinder Arora
 
19-graph1 (1).ppt
19-graph1 (1).ppt19-graph1 (1).ppt
19-graph1 (1).ppt
Himajanaidu2
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
KarunaBiswas3
 
Graphs
GraphsGraphs
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
chandrashekarr799
 

Similar to 8-Graph.ppt (20)

LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Graphss
GraphssGraphss
Graphss
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Graps 2
Graps 2Graps 2
Graps 2
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdf
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graphs
GraphsGraphs
Graphs
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
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
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 
UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
19-graph1 (1).ppt
19-graph1 (1).ppt19-graph1 (1).ppt
19-graph1 (1).ppt
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
 
Graphs
GraphsGraphs
Graphs
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
 

Recently uploaded

Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
Osisko Gold Royalties Ltd
 
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
f3wjr2q2
 
Corporate Presentation Probe June 2024.pdf
Corporate Presentation Probe June 2024.pdfCorporate Presentation Probe June 2024.pdf
Corporate Presentation Probe June 2024.pdf
Probe Gold
 
AGM Presentation Probe June 11 Final.pdf
AGM Presentation Probe June 11 Final.pdfAGM Presentation Probe June 11 Final.pdf
AGM Presentation Probe June 11 Final.pdf
Probe Gold
 
Cove Multifamily Income Fund 28 LLC IOI 3.3.2021 (1).pdf
Cove Multifamily Income Fund 28 LLC IOI 3.3.2021 (1).pdfCove Multifamily Income Fund 28 LLC IOI 3.3.2021 (1).pdf
Cove Multifamily Income Fund 28 LLC IOI 3.3.2021 (1).pdf
kboyd6
 
Mandalay Resouces June 2024 Investor Relations PPT
Mandalay Resouces June 2024 Investor Relations PPTMandalay Resouces June 2024 Investor Relations PPT
Mandalay Resouces June 2024 Investor Relations PPT
MandalayResources
 
mba project CRED.docx report for students final year
mba project CRED.docx report for students final yearmba project CRED.docx report for students final year
mba project CRED.docx report for students final year
JyothisaiBhavya4
 
Cleades robinson:The Diplomat is Blue
Cleades robinson:The Diplomat is BlueCleades robinson:The Diplomat is Blue
Cleades robinson:The Diplomat is Blue
Cleades Robinson
 
一比一原版(UAL毕业证)伦敦艺术大学毕业证如何办理
一比一原版(UAL毕业证)伦敦艺术大学毕业证如何办理一比一原版(UAL毕业证)伦敦艺术大学毕业证如何办理
一比一原版(UAL毕业证)伦敦艺术大学毕业证如何办理
nupyb
 
Collective Mining | Corporate Presentation - June 2024
Collective Mining  | Corporate Presentation - June 2024Collective Mining  | Corporate Presentation - June 2024
Collective Mining | Corporate Presentation - June 2024
CollectiveMining1
 
Cyberagent_For New Investors_EN_240424.pdf
Cyberagent_For New Investors_EN_240424.pdfCyberagent_For New Investors_EN_240424.pdf
Cyberagent_For New Investors_EN_240424.pdf
CyberAgent, Inc.
 
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdfZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
SOFTTECHHUB
 
UnityNet World Environment Day Abraham Project 2024 Press Release
UnityNet World Environment Day Abraham Project 2024 Press ReleaseUnityNet World Environment Day Abraham Project 2024 Press Release
UnityNet World Environment Day Abraham Project 2024 Press Release
LHelferty
 
Methanex Investor Presentation - April 2024
Methanex Investor Presentation - April 2024Methanex Investor Presentation - April 2024
Methanex Investor Presentation - April 2024
Methanex Corporation
 

Recently uploaded (14)

Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
 
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
 
Corporate Presentation Probe June 2024.pdf
Corporate Presentation Probe June 2024.pdfCorporate Presentation Probe June 2024.pdf
Corporate Presentation Probe June 2024.pdf
 
AGM Presentation Probe June 11 Final.pdf
AGM Presentation Probe June 11 Final.pdfAGM Presentation Probe June 11 Final.pdf
AGM Presentation Probe June 11 Final.pdf
 
Cove Multifamily Income Fund 28 LLC IOI 3.3.2021 (1).pdf
Cove Multifamily Income Fund 28 LLC IOI 3.3.2021 (1).pdfCove Multifamily Income Fund 28 LLC IOI 3.3.2021 (1).pdf
Cove Multifamily Income Fund 28 LLC IOI 3.3.2021 (1).pdf
 
Mandalay Resouces June 2024 Investor Relations PPT
Mandalay Resouces June 2024 Investor Relations PPTMandalay Resouces June 2024 Investor Relations PPT
Mandalay Resouces June 2024 Investor Relations PPT
 
mba project CRED.docx report for students final year
mba project CRED.docx report for students final yearmba project CRED.docx report for students final year
mba project CRED.docx report for students final year
 
Cleades robinson:The Diplomat is Blue
Cleades robinson:The Diplomat is BlueCleades robinson:The Diplomat is Blue
Cleades robinson:The Diplomat is Blue
 
一比一原版(UAL毕业证)伦敦艺术大学毕业证如何办理
一比一原版(UAL毕业证)伦敦艺术大学毕业证如何办理一比一原版(UAL毕业证)伦敦艺术大学毕业证如何办理
一比一原版(UAL毕业证)伦敦艺术大学毕业证如何办理
 
Collective Mining | Corporate Presentation - June 2024
Collective Mining  | Corporate Presentation - June 2024Collective Mining  | Corporate Presentation - June 2024
Collective Mining | Corporate Presentation - June 2024
 
Cyberagent_For New Investors_EN_240424.pdf
Cyberagent_For New Investors_EN_240424.pdfCyberagent_For New Investors_EN_240424.pdf
Cyberagent_For New Investors_EN_240424.pdf
 
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdfZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
 
UnityNet World Environment Day Abraham Project 2024 Press Release
UnityNet World Environment Day Abraham Project 2024 Press ReleaseUnityNet World Environment Day Abraham Project 2024 Press Release
UnityNet World Environment Day Abraham Project 2024 Press Release
 
Methanex Investor Presentation - April 2024
Methanex Investor Presentation - April 2024Methanex Investor Presentation - April 2024
Methanex Investor Presentation - April 2024
 

8-Graph.ppt

  • 2. Representations of Graphs •Adjacency Matrices •Adjacency Lists
  • 3. Adjacency Matrices Graphs G = (V, E) can be represented by adjacency matrices G[v1..v|V |, v1..v|V |], where the rows and columns are indexed by the nodes, and the entries G[vi, vj] represent the edges. In the case of unlabeled graphs, the entries are just boolean values. A B C D A 0 1 1 1 B 1 0 0 1 C 1 0 0 1 D 1 1 1 0
  • 4. Adjacency Matrices In case of labeled graphs, the labels themselves may be introduced into the entries. A B C D A 10 4 1 B 15 C 9 D Adjacency matrices require O(|V |2) space, and so they are space-efficient only when they are dense (that is, when the graphs have many edges). Time-wise, the adjacency matrices allow easy addition and deletion of edges.
  • 5. Adjacency Lists A representation of the graph consisting of a list of nodes, with each node containing a list of its neighboring nodes. This representation takes O(|V | + |E|) space.
  • 7. Depth-First Traversal • algorithm dft(x) visit(x) FOR each y such that (x,y) is an edge DO IF <y was not visited yet > THEN dft(y)
  • 8. Depth-First Traversal • A recursive algorithm implicitly recording a “backtracking” path from the root to the node currently under consideration
  • 13. Depth-First Traversal •Depth first search is another way of traversing graphs, which is closely related to preorder traversal of a tree. •the Breath-first search tree is typically "short and bushy", the DFS tree is typically "long and stringy".
  • 14. Breadth-First Traversal Visit the nodes at level i before the nodes of level i+1.
  • 15. Breadth-First Traversal visit(start node) queue <- start node WHILE queue is not empty DO x <- queue FOR each y such that (x,y) is an edge and y has not been visited yet DO visit(y) queue <- y END END
  • 19. Breadth-First Traversal • Each vertex is clearly • marked at most once, • added to the list at most once (since that happens only when it's marked), and • removed from the list at most once. Since the time to process a vertex is proportional to the length of its adjacency list, the total time for the whole algorithm is O(m). • A tree T constructed by the algorithm is called a breadth first search tree. • The traversal goes a level at a time, left to right within a level (where a level is defined simply in terms of distance from the root of the tree).
  • 20. Breadth-First Traversal • Every edge of G can be classified into one of three groups. • Some edges are in T themselves. • Some connect two vertices at the same level of T. • The remaining ones connect two vertices on two adjacent levels. It is not possible for an edge to skip a level. • Breadth-first search tree really is a shortest path tree starting from its root.
  • 21. Relation between BFS and DFS bfs(G) { list L = empty tree T = empty choose a starting vertex x search(x) while(L nonempty) remove edge (v,w) from start of L if w not yet visited { add (v,w) to T search(w) } } dfs(G) { list L = empty tree T = empty choose a starting vertex x search(x) while(L nonempty) remove edge (v,w) from end of L if w not yet visited { add (v,w) to T search(w) } } search(vertex v) { visit(v); for each edge (v,w) add edge (v,w) to end of L }
  • 22. Relation between BFS and DFS Both of these search algorithms now keep a list of edges to explore; the only difference between the two is while both algorithms adds items to the end of L, •BFS removes them from the beginning, which results in maintaining the list as a queue •DFS removes them from the end, maintaining the list as a stack.
  • 23. BFS and DFS in directed graphs The same search routines work essentially unmodified for directed graphs. The only difference is that when exploring a vertex v, we only want to look at edges (v,w) going out of v; we ignore the other edges coming into v. For BFS in directed graphs each edge of the graph either •connects two vertices at the same level •goes down exactly one level •goes up any number of levels. For DFS, each edge either connects an ancestor to •a descendant •a descendant to an ancestor •one node to a node in a previously visited subtree.
  • 25.
  • 29.
  • 32.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48. End
  • 49. End