0% found this document useful (0 votes)
15 views31 pages

Graph New

Data structure

Uploaded by

Mubashra Rashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
15 views31 pages

Graph New

Data structure

Uploaded by

Mubashra Rashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 31
Ways to represent a graph 2 Gizan st - Mare races wn be sf reignbow,/~ icsSoeetnae aeteeeeteeenel etentt Edge set - Store the pair of nodes/vertices connected with an edge. Eg - ((0,4), 0,4), (2, 4)) Other implementations to represent a graph also exists. For e.g. Compact list representation, cost adjacency list, cost adjacency matrix et. adjacency matrix is also a simple thing, you make a matrix like this, so for this graph i will make like this, i will write 01,2 here » Adjacency list = Mark the nodes with the list of its neighbors 0 9 © © —~ pay 1 + O-@-@/7 lit 2-7 @-O@°@ — (D) i hop iu understand this thing that t Adjacency matrix = A,= 1 for an edge between i andj, 0 otherwise! ETiTeR-\St MMA) RLM UAloM XXMONCA ON ORONCKEK ORCA RE KORCKONOKeKOneKeNeKe Ne) ‘ 1 Gey } Graph Traversal Graph traversal refers to the process of visiting (checking and/or Uptating) eros aE TCs ‘Sequence of steps known as Graph traversal algorithm can be used to traverse err) ‘Two Algorithms of Graph Traversal are: ee gee a) Depth First Search (DFS) Rein But this thing comes to your mind because you have a small graph. ed Graph Traversal 7 os Graph traversal refers to the process of visiting (checking and/or Updating) each vertex(node) in a graph. een ee ee eet rr) FOS TN Re ene re vo ee SN Ct eed Raa SS eat fee ache — PaO Which will definitely make sense in the upcoming videos. oy een ome BFS spanning tree (5 -Consder the graph shown at the right ‘= Me can stat with any source node 9Lets start wth 0 ‘= Trytoconstructa tree with 0 as the root as long as your node is there in the graph | BFS spanning tree ‘Consider the graph shown atthe right! We can start wth any source node Lets start wth 0 Try to construct tree with 0 as the root Mark al the sideways o duplicate edges (above a node) 0s dashed ‘This constructed tree is called as BFS Spanning Tree ‘= Level order traversal of @ BFS spanning tee is a valid BFS ‘waversal of @ raph! Mefed 2 BFS Traversal feat x _— (> ‘Consider the graph shown atthe right! Y 9 Wecan start with any source node {B> Lets start with O and insert inthe queve ‘= Visita the connected vertices and enqueue them for exploration 0 2 2ahse = Oto te oan o HLH KEE ps fa SRoTelm elect Col an Lei esicu traversal came BFS Traversal - Exploring 0 ‘Consider the graph shown atthe right! We can start wth any source node Lets start with 0 and insert tin the queue Visita the connected vertices and enqueue them for exploration (inary order) (is now explore Lot's go to the next in queue (3) Repeat the same for other elements in the queue are aol: mala BFS Traversal - Exploring 3 o }—{1 1.83 are also explored Repeat the same for other elements in the queue Visite geen Expl Surg BFS Traversal - Exploring 4, 5,6 ‘Consider the graph shown atthe right! We can start wth any source node Lets start with O and insert tin the queue Visit al the connected vertices and enqueue them for exploration (01s now explored Lt’ go to the next in queue (2) Repeat the same for other elements inthe queue Visited: 0, 1, 2, Exploration Quetiialielak! Important points 0 }—_{1 We can start with any vertex | There can be multiple BFS results for a given graph a) G . (Hint: Start with nodes other than 0) Now here we will talk about some important points Main Zoom Class a “~~ WITH QUIZ COMPETITION ~~~ THURSDAY, 14 December 2023 Qatar - 07:15 PM Pakistan - 09:15 PM << a India - 09:45 PM I started from one in this case Som F If it is not visited. if it is visited than this if will not work y DFS Procedure Start by putting any one of the graph's vertices on top of a stack, Take the top item of the stack and add it to the visited list Create a list of that vertex’s adjacent nodes. Add the ‘ones which aren't in the visited lst to the top of the stack Keep repeating steps 2 and 3 until the stack is empty. DFS Procedure Start by putting anyone of the graphs vertices ontop ora sac Take the top item of the stack and add it to the visited list Create a list of that vertex’s adjacent nodes. Add the ‘ones which aren't in the visited lst to the top of the stack. Keep repeating steps 2 and 3 until the stack is empty. Be Viskd: 01234562 ute B OR LOSI Ni 042 ° DFS Procedure e 4 Atart by putting any one ofthe graphs vertices on top of a stack, 2-“ate the top tem ofthe stack and ad tothe vised 2 list 3-Create a list of that vertex’s adjacent nodes. Add the ‘ones which aren't in the visited lst to the top of the {6} stack [3] 4. Keep repeating steps 2 and 3 until the stack is empty. Bp Vist; © 12345 62 Ni o4 ° DFS Procedure 3 Atart by putting any one ofthe graphs vertices on top of a stack, _2--Take the top item of the stack and add it to the visited list _3- Create a list ofthat vertex’s adjacent nodes. Add the @} ‘ones which aren't in the visited lst to the top of the 0] stack [3 feteenerenrte teresa sten renee sere ind: 0.1 234562, DFS Procedure - Step 1 1. Start by putting any one of the graph's vertices on top of a stack Lets put O into the stack! Visited: DFS Procedure - Step 2 Start by putting any one of the graph's vertices on top of a stack Lets put 0 into the stack! 2. Take the top item of the stack and add it to the visited list ~0is now visited! Visited: 0 DFS Procedure - Step 3 1. Start by putting any one of the graph's vertices on top Of a stack ~ Lets put O into the stack! Take the top item of the stack and add it to the visited list Create a list of that vertex’s adjacent nodes. Add the ‘ones which aren't in the visited lst to the top of the stack. gs Visited: 0, DFS Procedure - Step 2 1. Take the top item of the stack and add it to the visited list 1 is now visited! Visited: 0, 1 DFS Procedure - Step 2 1. Take the top item of the stack and add it to the visited list - 1 is now visited! Visited: 0, 1,2, 4 Soho le ail NS Oe UR neat ries Succeed et ord (oD ae = ao email Subgraphs ‘= Asubgraph of a graph G is a graph whose vertices and edges are subsets of the original graph G. A — G 5 e Este MiB mUcel MVM Le neal cM Amn -Re lalla MUnr-(a Une Relea o) graph okay, i hope it is clear that some nodes will come from our original Connected and Complete Graphs. = _Aconnected graphiis graph that is connected in tte Sense of a topological space, i.e., there is a ath from any point to any other point in the graph. ‘Aggraph that is not connected is said to be disconnected, ‘A complete graph is a simple undirected graph in which every pair of distinct vertices is connected by a unique edge Quick Quiz: f= Is the graph on the right @ connected graph? So i will complete graph, so it is a complete graph okay, if i took 4 nodes, so 1,2,3,4 are my 4 nodes, one 1, one 2, one 3 and one 4 a A afc) mi er- es) oval area le ciota = Asubgraph of a graph G is a graph whose vertices and édges aré subsets Crue Pu Eee AS graph G iff (if and only if} 4 All vertices of G must be presént in S eo Ena No of spanning trees for Complete Graphs \# A complete graph has n*? Spanning Trees where nis the number of vertices in the graph Quick Quiz: ‘= Draw 3 spanning trees for the graph at the right

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy