1 / 14

Graphs Representation, BFS, DFS

Graphs Representation, BFS, DFS. Graph representations. Nodes or vertices Edges Different types of graphs Weighted vs. unweighted Undirected vs. directed Multiple edges (from one node to another) or not Self-loops (edge going to same node) or not. Degree.

rpuckett
Download Presentation

Graphs Representation, BFS, DFS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GraphsRepresentation, BFS, DFS

  2. Graph representations • Nodes or vertices • Edges • Different types of graphs • Weighted vs. unweighted • Undirected vs. directed • Multiple edges (from one node to another) or not • Self-loops (edge going to same node) or not

  3. Degree • Vertex degree: number of edges for that vertex • Undirected • Degree is the number of edges leaving vertex • Self loops count twice • Number of edges = 2*sum of vertex degrees • Directed • In-degree (incoming edge count) and out-degree (outgoing edge count) • Sum of all in-degrees (across all vertices) equals sum of all out-degrees

  4. Traveling a graph • Path/walk/trail • Follow edges along vertices • Could repeat edge unless stated otherwise • Cycle/circuit/tour • Path that leads back to the original vertex • Usually assumes won’t follow same edge, but not always

  5. Storing Graphs • 3 main methods • Edge List • Adjacency List • Adjacency Matrix

  6. Storing graphs • Edge list • Keep a list of all edges • Store start, end, weight • Good if you need to work with all the edges at once • Kruskal’s MST algorithm • Not good at all if you want to know the adjacent edges for any one vertex

  7. Edge List 6 1 2 Edge from 1 to 4, weight 8 5 3 8 Edge from 2 to 4, weight 5 3 4 9 Edge from 1 to 3, weight 3 Edge from 3 to 4, weight 9 Edge from 1 to 2, weight 6

  8. Storing graphs • Adjacency list • Keep a list of edges in each vertex • Can be used for directed or undirected • Undirected – must keep consistent on both ends • Can keep weights per edge in the node list • Or, store pointer to a light edge structure • Good to find and iterate through edges for any vertex • Typical “default” implementation for a graph

  9. Adjacency List 6 1 2 Node 1 Edge to 4 Weight 8 Edge to 3 Weight 3 Edge to 2 Weight 6 5 3 8 Node 2 Edge to 1 Weight 6 Edge to 4 Weight 5 3 4 9 Edge to 1 Weight 3 Edge to 4 Weight 9 Node 3 Node 4 Edge to 1 Weight 8 Edge to 2 Weight 5 Edge to 3 Weight 9

  10. Storing graphs • Adjacency matrix • Store matrix indicating edges between vertices (rows/columns) • Directed: rows are from, columns are to • Value in the cell can be the weight of the edge • Bad if the graph is sparse, or too large • Can sometimes phrase graph calculations as matrix calculations this way; then, it can be more efficient to compute with

  11. Adjacency Matrix 6 1 2 5 3 8 3 4 9

  12. Breadth First/Depth First Search • Basic Overview: BFS keeps a QUEUE, DFS keeps a STACK • Get the next node (or, at beginning, starting node) • If node was already visited, then ignore it • Otherwise, mark it as visited, then for all adjacent vertices, put them on the queue/stack • Can store information like distance/time when a node is discovered • For DFS, can store time first found, and time when “finished”

  13. Depth-First Search • Keep global list of visited T/F (or a number indicating which “tree” it is part of) • Recursive function: • Mark current node visited • For all adjacent edges: • If the node is unvisited, visit it • Basically, keep a stack of nodes that you want to visit • Forms the basis for several other operations

  14. Breadth-first search • Keep a “distance” for each node, initialized to infinity • Keep queue of nodes to process, start with intro node • Repeatedly pop a node off the queue • Go through list of adjacent nodes • If node is infinity away, then set its distance to current distance plus one and add to queue • Has a few good uses • Shortest path in an undirected graph

More Related