Understanding Graphs and Traversals in Computer Science

graphs graphs cse 332 section 6 n.w
1 / 35
Embed
Share

Explore the world of graphs, vertices, edges, and graph traversals in computer science. Learn about terminology, directed vs. undirected graphs, and depth-first and breadth-first search algorithms. Dive into the essentials of graph theory through detailed explanations and visual aids.

  • Graph Theory
  • Traversals
  • Computer Science
  • Algorithms

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Graphs Graphs CSE 332 Section 6 Slides by James Richie Sulaeman

  2. Graphs Graphs

  3. Graphs Graphs A graph is a set of vertices A vertex is also known as a node An edge is represented as a pair of vertices vertices connected by edges edges A C B D vertices vertices F edges edges E example of a undirected, unweighted, cyclic graph

  4. Graph Terminology Graph Terminology Degree of vertex V Number of edges connected to vertex V In-degree: number of edges going into vertex V Out-degree: number of edges going out of vertex V Weight of edge e Numerical value/cost associated with traversing edge e Path A sequence of adjacent vertices connected by edges Cycle A path that begins and ends at the same vertex

  5. Graph Terminology Graph Terminology Directed vs. undirected graphs Edges can have direction (i.e. bidirectional vs. unidirectional) Weighted vs. unweighted graphs Edges can have weights/costs (e.g. how many minutes to go from vertex A to B) Cyclic vs. acyclic graphs Graph contains a cycle A A A A 7 3 3 7 1 B C B C B C B C 5 5 directed, unweighted, acyclic graph directed, weighted, cyclic graph undirected, unweighted, acyclic graph undirected, weighted, cyclic graph

  6. Graph Traversals Graph Traversals

  7. Graph Traversals Graph Traversals How do we iterate through a graph? Depth First Search (DFS) Explores the graph by going as deep as possible Can be implemented recursively/by using a stack O(|V| + |E|) runtime Depth First Search (DFS) Breadth First Search (BFS) Explores the graph level by level Implemented using a queue Finds the shortest path in an unweighted, acyclic graph O(|V| + |E|) runtime Breadth First Search (BFS)

  8. Depth First Search Depth First Search Explores the graph by going as deep as possible Implemented recursively/by using a stack O(|V| + |E|) runtime DFS(Graph g, Vertex curr): mark curr as visited for (v : neighbors(current)): if (!v marked visited ) dfs(g, v) mark curr as done ; Depth First Search (DFS)

  9. Breadth First Search Breadth First Search Explores the graph level by level Implemented using a queue Finds the shortest path in an unweighted, acyclic graph O(|V| + |E|) runtime BFS(Vertex start): initialize queue q to hold start mark start as visited while q is not empty: vertex v = q.dequeue() for each neighbour u of v: if u is not visited: mark u as visited predecessor[u] = v add u to q Breadth First Search (BFS)

  10. Problem 0 Problem 0 DFS(Graph g, Vertex curr): mark curr as visited for (v : neighbors(current)): if (!v marked visited ) dfs(g, v) mark curr as done ;

  11. Problem 0 Problem 0 Vertex Vertex Visited? Visited? Done? Done? S S T S S No Yes No T T No No X X No No Y Z X Y Y No No Z Z No No StackFrame (curr): Build stack frame of dfs(g, S) Mark vertex S as visited S

  12. Problem 0 Problem 0 Vertex Vertex Visited? Visited? Done? Done? T T S S S S Yes No T T No Yes No X X No No Y Z X Y Y No No Z Z No No StackFrame (curr): S Call dfs on unvisited neighbor T Mark vertex T as visited T S

  13. Problem 0 Problem 0 Vertex Vertex Visited? Visited? Done? Done? S T T S S Yes No T T Yes No X X No Yes No X X Y Z Y Y No No Z Z No No StackFrame (curr): T Call dfs on unvisited neighbor X Mark vertex X as visited T X S

  14. Problem 0 Problem 0 Vertex Vertex Visited? Visited? Done? Done? S T S S Yes No T T Yes No X X Yes No Yes Y Z X Y Y No No Z Z No No StackFrame (curr): No recursive call in X: all neighbors are visited Mark vertex X as done, exit X stack frame T X X S

  15. Problem 0 Problem 0 Vertex Vertex Visited? Visited? Done? Done? S T T S S Yes No T T Yes No X X Yes Yes Y Y Z X Y Y No Yes No Z Z No No StackFrame (curr): T Call dfs on unvisited neighbor Y Mark vertex Y as visited T S X Y

  16. Problem 0 Problem 0 Vertex Vertex Visited? Visited? Done? Done? S T S S Yes No T T Yes No X X Yes Yes Y Y Z Z X Y Y Yes No Z Z No Yes No StackFrame (curr): Y Call dfs on unvisited neighbor Z Mark vertex Z as visited T S X Y Z

  17. Problem 0 Problem 0 Vertex Vertex Visited? Visited? Done? Done? S T S S Yes No T T Yes No X X Yes Yes Y Z X Y Y Yes No Z Z Yes No Yes StackFrame (curr): No recursive call in Z: all neighbors are visited Mark vertex Z as done, exit Z stack frame T S X Y Z Z

  18. Problem 0 Problem 0 Vertex Vertex Visited? Visited? Done? Done? S T S S Yes No T T Yes No X X Yes Yes Y Z X Y Y Yes No Yes Z Z Yes Yes StackFrame (curr): Finish all recursive in Y: all neighbors are visited Mark vertex Y as done, exit Y stack frame T S X Y Y Z

  19. Problem 0 Problem 0 Vertex Vertex Visited? Visited? Done? Done? S T S S Yes No No Yes T T Yes X X Yes Yes Y Z X Y Y Yes Yes Z Z Yes Yes StackFrame (curr): Finish all recursive in T: all neighbors are visited Mark vertex T as done, exit T stack frame T T S X Y Z

  20. Problem 0 Problem 0 Vertex Vertex Visited? Visited? Done? Done? S T No Yes S S Yes T T Yes Yes X X Yes Yes Y Z X Y Y Yes Yes Z Z Yes Yes StackFrame (curr): Finish all recursive in S: all neighbors are visited Mark vertex S as done, exit S stack frame S S T X Y Z

  21. Problem 1 Problem 1 BFS(Vertex start): initialize queue q to hold start mark start as visited while q is not empty: vertex v = q.dequeue() for each neighbour u of v: if u is not visited: mark u as visited predecessor[u] = v add u to q

  22. Problem 1 Problem 1 Vertex Vertex Predecessor Predecessor Visited? Visited? S S T S S No Yes T T No X X No Y Z X Y Y No Z Z No Queue: Initialize queue to hold starting vertex S Mark vertex S as visited S front back

  23. Problem 1 Problem 1 Vertex Vertex Predecessor Predecessor Visited? Visited? S S S T T S S Yes No Yes T T S X X No Y Y Z X Y Y S No Yes Z Z No Queue: Dequeue vertex S Add neighbors T, Y to the queue T Y front back

  24. Problem 1 Problem 1 Vertex Vertex Predecessor Predecessor Visited? Visited? S S T T T S S Yes T T S Yes X X T No Yes Y Y Z Z X X Y Y S Yes Z Z T No Yes Queue: Dequeue vertex T Add neighbors X, Z to the queue (ignore Y since already visited) Y X Z front back

  25. Problem 1 Problem 1 Vertex Vertex Predecessor Predecessor Visited? Visited? S S T T S S Yes T T S Yes X X T Yes Y Y Y Z Z X X Y Y S Yes Z Z T Yes Queue: Dequeue vertex Y Add neighbors to the queue (nothing happens since all already visited) X Z front back

  26. Problem 1 Problem 1 Vertex Vertex Predecessor Predecessor Visited? Visited? S S T T S S Yes T T S Yes X X T Yes Y Y Z Z X X X Y Y S Yes Z Z T Yes Queue: Dequeue vertex X Add neighbors to the queue (nothing happens since all already visited) Z front back

  27. Problem 1 Problem 1 Vertex Vertex Predecessor Predecessor Visited? Visited? S S T T S S Yes T T S Yes X X T Yes Y Y Z Z Z X X Y Y S Yes Z Z T Yes Queue: Dequeue vertex Z Add neighbors to the queue (nothing happens since all already visited) front back

  28. Problem 1 Problem 1 Vertex Vertex Predecessor Predecessor Visited? Visited? S S T T S S Yes T T S Yes X X T Yes Y Y Z Z X X Y Y S Yes Z Z T Yes Queue: Queue is empty; we are done front back

  29. BFS Table Interpretation BFS Table Interpretation

  30. BFS Table Interpretation BFS Table Interpretation How to check if a path exists from the start node to a target node? Vertex Vertex Predecessor Predecessor Visited? Visited? S S Yes A path exists if and only if has a predecessor in the table if and only if the target node T T S Yes How to find a path from the start node to a target node? Locate the target node in the table Backtrack through its predecessors until you reach the start node The sequence of predecessors form a path from the start to the target Will be the shortest path by edge count (but Will be the shortest path by edge count (but not necessarily sum of edge costs) not necessarily sum of edge costs) X X T Yes Y Y S Yes Z Z T Yes

  31. BFS/DFS Useful Properties BFS/DFS Useful Properties

  32. BFS BFS - - Shortest Path Shortest Path BFS always always returns the shortest path from source to any other vertex by edge count source to any other vertex by edge count! Intuition: Each step push neighbors that are one edge away, onto a queue. Because we use a Because we use a queue, process the vertices 1 edge away, before vertices farther away Each vertex s predecessor in the table is the one which initially pushes it onto the stack (earliest/shortest path) shortest path from queue, we must

  33. DFS DFS Detect Cycle Detect Cycle Start from A - Use DFS to detect cycle by finding a back edge - a back edge is an edge that connects a node to one of its ancestors in the current recursion stack. A E B D C

  34. DFS DFS Detect Cycle Detect Cycle Start from A Vertex: D Mark as done Edge points to a visited node, back edge detected! Edge points to a visited node, back edge A Vertex: E Mark as done Vertex: D Mark D as visited Vertex: C Mark C as visited Mark E as visited detected! E Vertex: C Mark C as visited Vertex: B Vertex: B Go to next unvisited Vertex: E B Vertex: B Mark B as visited Vertex: A Vertex: A D C Vertex: A Mark A as visited Mark A as visited

  35. Thank You! Thank You!

Related


More Related Content