cse 373 more on graphs dfs and bfs
play

CSE 373: More on graphs; DFS and BFS Michael Lee Wednesday, Feb 14, - PowerPoint PPT Presentation

CSE 373: More on graphs; DFS and BFS Michael Lee Wednesday, Feb 14, 2018 1 Warmup Warmup: Discuss with your neighbor: A simple graph is a graph that has no self-loops and no parallel edges. is the maximum number of edges it can have, in


  1. CSE 373: More on graphs; DFS and BFS Michael Lee Wednesday, Feb 14, 2018 1

  2. Warmup Warmup: Discuss with your neighbor: A simple graph is a graph that has no self-loops and no parallel edges. is the maximum number of edges it can have, in terms of x ? Each vertex can connect to x other vertices, so x x . with y edges. What is the maximum number of vertices it can have, in terms of y ? Infjnite: just keep adding nodes with no edges attached. 2 ◮ Remind your neighbor: what is a simple graph ? ◮ Suppose we have a simple, directed graph with x nodes. What ◮ Now, suppose we have a difgerent simple, undirected graph

  3. Warmup Warmup: Discuss with your neighbor: A simple graph is a graph that has no self-loops and no parallel edges. is the maximum number of edges it can have, in terms of x ? with y edges. What is the maximum number of vertices it can have, in terms of y ? Infjnite: just keep adding nodes with no edges attached. 2 ◮ Remind your neighbor: what is a simple graph ? ◮ Suppose we have a simple, directed graph with x nodes. What Each vertex can connect to x − 1 other vertices, so x ( x − 1) . ◮ Now, suppose we have a difgerent simple, undirected graph

  4. of what it would be if the graph were directed. So, x x Warmup: can just keep adding more and more self-loops. Note that if previously. Either way, it’s still infjnite, for the same reasons given have? What if the graph is not simple? with y edges. What is the maximum number of vertices it can , there can’t be any edges at all. x , we Some follow-up questions: If the graph is not simple, it’s infjnite: assuming x . If the graph is simple, the max number of edges is exactly half the graph is not simple? What is the maximum number of edges it can have? What if 3 ◮ Suppose we have a simple, undirected graph with x nodes. ◮ Now, suppose we have a difgerent simple, undirected graph

  5. Warmup: Some follow-up questions: previously. Either way, it’s still infjnite, for the same reasons given have? What if the graph is not simple? with y edges. What is the maximum number of vertices it can can just keep adding more and more self-loops. Note that if 3 . If the graph is simple, the max number of edges is exactly half the graph is not simple? What is the maximum number of edges it can have? What if ◮ Suppose we have a simple, undirected graph with x nodes. of what it would be if the graph were directed. So, x ( x − 1) 2 If the graph is not simple, it’s infjnite: assuming x > 0 , we x = 0 , there can’t be any edges at all. ◮ Now, suppose we have a difgerent simple, undirected graph

  6. Summary V To put it another way, sparse graphs have “few” edges. , we sau the graph is sparse . V If E Sparse graph To put it another way, dense graphs have “lots of edges” , we say the graph is dense . If E What did we learn? Dense graph , for both directed and undirected graphs. V know E In simple graphs, if we know V is some fjxed value, we also vertices are independent. 4 ◮ In graphs with no restrictions, number of edges and number of

  7. Summary V To put it another way, sparse graphs have “few” edges. , we sau the graph is sparse . V If E Sparse graph To put it another way, dense graphs have “lots of edges” , we say the graph is dense . If E What did we learn? Dense graph , for both directed and undirected graphs. vertices are independent. 4 ◮ In graphs with no restrictions, number of edges and number of ◮ In simple graphs, if we know | V | is some fjxed value, we also � | V | 2 � know | E | ∈ O

  8. Summary What did we learn? To put it another way, sparse graphs have “few” edges. , we sau the graph is sparse . V If E Sparse graph To put it another way, dense graphs have “lots of edges” , we say the graph is dense . 4 Dense graph , for both directed and undirected graphs. vertices are independent. ◮ In graphs with no restrictions, number of edges and number of ◮ In simple graphs, if we know | V | is some fjxed value, we also � | V | 2 � know | E | ∈ O � | V | 2 � If | E | ∈ Θ

  9. Summary , for both directed and undirected graphs. To put it another way, sparse graphs have “few” edges. Sparse graph To put it another way, dense graphs have “lots of edges” , we say the graph is dense . What did we learn? Dense graph 4 vertices are independent. ◮ In graphs with no restrictions, number of edges and number of ◮ In simple graphs, if we know | V | is some fjxed value, we also � | V | 2 � know | E | ∈ O � | V | 2 � If | E | ∈ Θ If | E | ∈ O ( | V | ) , we sau the graph is sparse .

  10. How do we represent graphs in code? So, how do we actually represent graphs in code? Two common approaches, with difgerent tradeofgs: Adjacency matrix Adjacency list 5

  11. How do we represent graphs in code? So, how do we actually represent graphs in code? Two common approaches, with difgerent tradeofgs: 5 ◮ Adjacency matrix ◮ Adjacency list

  12. Adjacency matrix b a b c d d c a Core idea: d c b a 6 ◮ Assign each node a number from 0 to | V | − 1 ◮ Create a | V | × | V | nested array of booleans or ints ◮ If ( x , y ) ∈ E , then nestedArray[x][y] == true

  13. Adjacency matrix b a b c d d c a Core idea: d c b a 6 ◮ Assign each node a number from 0 to | V | − 1 ◮ Create a | V | × | V | nested array of booleans or ints ◮ If ( x , y ) ∈ E , then nestedArray[x][y] == true

  14. Adjacency list How much space do we use? edges, not easily Self-loops yes, parallel Can we handle self-loops and parallel edges? Dense ones Is this better for sparse or dense graphs? V 7 What is the worst-case runtime to: V V ◮ Get out-edges: ◮ Get in-edges: ◮ Decide if an edge exists: ◮ Insert an edge: ◮ Delete an edge:

  15. Adjacency list What is the worst-case runtime to: Is this better for sparse or dense graphs? Dense ones Can we handle self-loops and parallel edges? Self-loops yes, parallel edges, not easily 7 ◮ Get out-edges: O ( | V | ) ◮ Get in-edges: O ( | V | ) ◮ Decide if an edge exists: O (1) ◮ Insert an edge: O (1) ◮ Delete an edge: O (1) � | V | 2 � How much space do we use? O

  16. On a higher level: represent as IDictionary<Vertex, Edges> . Adjacency list Core idea: a b c d 8 ◮ Assign each node a number from 0 to | V | − 1 ◮ Create an array of size | V | ◮ Each element in the array stores its out edges in a list or set

  17. Adjacency list Core idea: a b c d 8 ◮ Assign each node a number from 0 to | V | − 1 ◮ Create an array of size | V | ◮ Each element in the array stores its out edges in a list or set ◮ On a higher level: represent as IDictionary<Vertex, Edges> .

  18. Adjacency list Core idea: a b c d 8 ◮ Assign each node a number from 0 to | V | − 1 ◮ Create an array of size | V | ◮ Each element in the array stores its out edges in a list or set ◮ On a higher level: represent as IDictionary<Vertex, Edges> .

  19. Adjacency list We can store edges using either sets or lists. Answer these questions for both. What is the worst-case runtime to: How much space do we use? Is this better for sparse or dense graphs? Can we handle self-loops and parallel edges? 9 ◮ Get out-edges: ◮ Get in-edges: ◮ Decide if an edge exists: ◮ Insert an edge: ◮ Delete an edge:

  20. Which do we pick? So which do we pick? Observations: Most graphs are sparse If we implement adjacency lists using sets, we can get comparable worst-case performance So by default, pick adjacency lists. 10

  21. Which do we pick? So which do we pick? Observations: comparable worst-case performance So by default, pick adjacency lists. 10 ◮ Most graphs are sparse ◮ If we implement adjacency lists using sets, we can get

  22. Which do we pick? So which do we pick? Observations: comparable worst-case performance So by default, pick adjacency lists. 10 ◮ Most graphs are sparse ◮ If we implement adjacency lists using sets, we can get

  23. Walks and paths d e d c b a Path e c Walk b a Walk A path is a walk that never visits the same vertex twice. Path More intuitively, a walk is one continous line following the edges. 11 A walk is a list of vertices v 0 , v 1 , v 2 , . . . , v n where if i is some int where 0 ≤ i < v n , every pair ( v i , v i +1 ) ∈ E is true.

  24. Walks and paths d e d c b a Path e c Walk b a Walk A path is a walk that never visits the same vertex twice. Path More intuitively, a walk is one continous line following the edges. 11 A walk is a list of vertices v 0 , v 1 , v 2 , . . . , v n where if i is some int where 0 ≤ i < v n , every pair ( v i , v i +1 ) ∈ E is true.

  25. Walks and paths d e d c b a Path Path or walk? e c Walk b a Walk Path or walk? A path is a walk that never visits the same vertex twice. Path More intuitively, a walk is one continous line following the edges. 11 A walk is a list of vertices v 0 , v 1 , v 2 , . . . , v n where if i is some int where 0 ≤ i < v n , every pair ( v i , v i +1 ) ∈ E is true.

  26. Walks and paths d e d c b a Path e c Walk b a Walk A path is a walk that never visits the same vertex twice. Path More intuitively, a walk is one continous line following the edges. 11 A walk is a list of vertices v 0 , v 1 , v 2 , . . . , v n where if i is some int where 0 ≤ i < v n , every pair ( v i , v i +1 ) ∈ E is true.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend