SlideShare uma empresa Scribd logo
1 de 49
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

Mais conteúdo relacionado

Semelhante a 8-Graph.ppt

Semelhante a 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
 

Último

Sustainability Leadership, April 26 2024
Sustainability Leadership, April 26 2024Sustainability Leadership, April 26 2024
Sustainability Leadership, April 26 2024TeckResourcesLtd
 
Call Girls In Amritsar 💯Call Us 🔝 76967 34778🔝 💃 Independent Escort In Amritsar
Call Girls In Amritsar 💯Call Us 🔝 76967 34778🔝 💃 Independent Escort In AmritsarCall Girls In Amritsar 💯Call Us 🔝 76967 34778🔝 💃 Independent Escort In Amritsar
Call Girls In Amritsar 💯Call Us 🔝 76967 34778🔝 💃 Independent Escort In Amritsaronly4webmaster01
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Shamshabad high-profile Call ...
VIP 7001035870 Find & Meet Hyderabad Call Girls Shamshabad high-profile Call ...VIP 7001035870 Find & Meet Hyderabad Call Girls Shamshabad high-profile Call ...
VIP 7001035870 Find & Meet Hyderabad Call Girls Shamshabad high-profile Call ...aditipandeya
 
BDSM⚡Call Girls in Hari Nagar Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Hari Nagar Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Hari Nagar Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Hari Nagar Delhi >༒8448380779 Escort ServiceDelhi Call girls
 
Teck Investor Presentation, April 24, 2024
Teck Investor Presentation, April 24, 2024Teck Investor Presentation, April 24, 2024
Teck Investor Presentation, April 24, 2024TeckResourcesLtd
 
Malad Escorts, (Pooja 09892124323), Malad Call Girls Service
Malad Escorts, (Pooja 09892124323), Malad Call Girls ServiceMalad Escorts, (Pooja 09892124323), Malad Call Girls Service
Malad Escorts, (Pooja 09892124323), Malad Call Girls ServicePooja Nehwal
 
Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024CollectiveMining1
 
VIP Call Girl Amritsar 7001035870 Enjoy Call Girls With Our Escorts
VIP Call Girl Amritsar 7001035870 Enjoy Call Girls With Our EscortsVIP Call Girl Amritsar 7001035870 Enjoy Call Girls With Our Escorts
VIP Call Girl Amritsar 7001035870 Enjoy Call Girls With Our Escortssonatiwari757
 
《加州大学圣克鲁兹分校学位证书复制》Q微信741003700美国学历疑难问题指南|挂科被加州大学圣克鲁兹分校劝退没有毕业证怎么办?《UCSC毕业证购买|加...
《加州大学圣克鲁兹分校学位证书复制》Q微信741003700美国学历疑难问题指南|挂科被加州大学圣克鲁兹分校劝退没有毕业证怎么办?《UCSC毕业证购买|加...《加州大学圣克鲁兹分校学位证书复制》Q微信741003700美国学历疑难问题指南|挂科被加州大学圣克鲁兹分校劝退没有毕业证怎么办?《UCSC毕业证购买|加...
《加州大学圣克鲁兹分校学位证书复制》Q微信741003700美国学历疑难问题指南|挂科被加州大学圣克鲁兹分校劝退没有毕业证怎么办?《UCSC毕业证购买|加...wyqazy
 
Collective Mining | Corporate Presentation - May 2024
Collective Mining | Corporate Presentation - May 2024Collective Mining | Corporate Presentation - May 2024
Collective Mining | Corporate Presentation - May 2024CollectiveMining1
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Banjara Hills high-profile Ca...
VIP 7001035870 Find & Meet Hyderabad Call Girls Banjara Hills high-profile Ca...VIP 7001035870 Find & Meet Hyderabad Call Girls Banjara Hills high-profile Ca...
VIP 7001035870 Find & Meet Hyderabad Call Girls Banjara Hills high-profile Ca...aditipandeya
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Abids high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls Abids high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls Abids high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls Abids high-profile Call Girladitipandeya
 

Último (20)

Sustainability Leadership, April 26 2024
Sustainability Leadership, April 26 2024Sustainability Leadership, April 26 2024
Sustainability Leadership, April 26 2024
 
Rohini Sector 17 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 17 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 17 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 17 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
(👉゚9999965857 ゚)👉 VIP Call Girls Greater Noida 👉 Delhi 👈 : 9999 Cash Payment...
(👉゚9999965857 ゚)👉 VIP Call Girls Greater Noida  👉 Delhi 👈 : 9999 Cash Payment...(👉゚9999965857 ゚)👉 VIP Call Girls Greater Noida  👉 Delhi 👈 : 9999 Cash Payment...
(👉゚9999965857 ゚)👉 VIP Call Girls Greater Noida 👉 Delhi 👈 : 9999 Cash Payment...
 
Vip Call Girls Hauz Khas ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Hauz Khas ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Hauz Khas ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Hauz Khas ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Call Girls In Amritsar 💯Call Us 🔝 76967 34778🔝 💃 Independent Escort In Amritsar
Call Girls In Amritsar 💯Call Us 🔝 76967 34778🔝 💃 Independent Escort In AmritsarCall Girls In Amritsar 💯Call Us 🔝 76967 34778🔝 💃 Independent Escort In Amritsar
Call Girls In Amritsar 💯Call Us 🔝 76967 34778🔝 💃 Independent Escort In Amritsar
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Shamshabad high-profile Call ...
VIP 7001035870 Find & Meet Hyderabad Call Girls Shamshabad high-profile Call ...VIP 7001035870 Find & Meet Hyderabad Call Girls Shamshabad high-profile Call ...
VIP 7001035870 Find & Meet Hyderabad Call Girls Shamshabad high-profile Call ...
 
BDSM⚡Call Girls in Hari Nagar Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Hari Nagar Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Hari Nagar Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Hari Nagar Delhi >༒8448380779 Escort Service
 
Call Girls In South Delhi 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In South Delhi 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICECall Girls In South Delhi 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In South Delhi 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
 
Teck Investor Presentation, April 24, 2024
Teck Investor Presentation, April 24, 2024Teck Investor Presentation, April 24, 2024
Teck Investor Presentation, April 24, 2024
 
Malad Escorts, (Pooja 09892124323), Malad Call Girls Service
Malad Escorts, (Pooja 09892124323), Malad Call Girls ServiceMalad Escorts, (Pooja 09892124323), Malad Call Girls Service
Malad Escorts, (Pooja 09892124323), Malad Call Girls Service
 
Vip Call Girls Vasant Kunj ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Vasant Kunj ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Vasant Kunj ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Vasant Kunj ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024Collective Mining | Corporate Presentation - April 2024
Collective Mining | Corporate Presentation - April 2024
 
(👉゚9999965857 ゚)👉 VIP Call Girls Friends Colony 👉 Delhi 👈 : 9999 Cash Payment...
(👉゚9999965857 ゚)👉 VIP Call Girls Friends Colony 👉 Delhi 👈 : 9999 Cash Payment...(👉゚9999965857 ゚)👉 VIP Call Girls Friends Colony 👉 Delhi 👈 : 9999 Cash Payment...
(👉゚9999965857 ゚)👉 VIP Call Girls Friends Colony 👉 Delhi 👈 : 9999 Cash Payment...
 
VIP Call Girl Amritsar 7001035870 Enjoy Call Girls With Our Escorts
VIP Call Girl Amritsar 7001035870 Enjoy Call Girls With Our EscortsVIP Call Girl Amritsar 7001035870 Enjoy Call Girls With Our Escorts
VIP Call Girl Amritsar 7001035870 Enjoy Call Girls With Our Escorts
 
《加州大学圣克鲁兹分校学位证书复制》Q微信741003700美国学历疑难问题指南|挂科被加州大学圣克鲁兹分校劝退没有毕业证怎么办?《UCSC毕业证购买|加...
《加州大学圣克鲁兹分校学位证书复制》Q微信741003700美国学历疑难问题指南|挂科被加州大学圣克鲁兹分校劝退没有毕业证怎么办?《UCSC毕业证购买|加...《加州大学圣克鲁兹分校学位证书复制》Q微信741003700美国学历疑难问题指南|挂科被加州大学圣克鲁兹分校劝退没有毕业证怎么办?《UCSC毕业证购买|加...
《加州大学圣克鲁兹分校学位证书复制》Q微信741003700美国学历疑难问题指南|挂科被加州大学圣克鲁兹分校劝退没有毕业证怎么办?《UCSC毕业证购买|加...
 
Collective Mining | Corporate Presentation - May 2024
Collective Mining | Corporate Presentation - May 2024Collective Mining | Corporate Presentation - May 2024
Collective Mining | Corporate Presentation - May 2024
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Banjara Hills high-profile Ca...
VIP 7001035870 Find & Meet Hyderabad Call Girls Banjara Hills high-profile Ca...VIP 7001035870 Find & Meet Hyderabad Call Girls Banjara Hills high-profile Ca...
VIP 7001035870 Find & Meet Hyderabad Call Girls Banjara Hills high-profile Ca...
 
Preet Vihar (Delhi) 9953330565 Escorts, Call Girls Services
Preet Vihar (Delhi) 9953330565 Escorts, Call Girls ServicesPreet Vihar (Delhi) 9953330565 Escorts, Call Girls Services
Preet Vihar (Delhi) 9953330565 Escorts, Call Girls Services
 
Call Girls Service Green Park @9999965857 Delhi 🫦 No Advance VVIP 🍎 SERVICE
Call Girls Service Green Park @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SERVICECall Girls Service Green Park @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SERVICE
Call Girls Service Green Park @9999965857 Delhi 🫦 No Advance VVIP 🍎 SERVICE
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Abids high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls Abids high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls Abids high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls Abids high-profile Call Girl
 

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