Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Representation, BFS, DFS

Similar presentations


Presentation on theme: "Graphs Representation, BFS, DFS"— Presentation transcript:

1 Graphs Representation, BFS, DFS

2 Graph representations
Nodes or vertices Edges Weighted vs. unweighted Undirected vs. directed Multiple edges or not Self-loops or not

3 Degree Vertex degree: edges for that vertex Undirected Directed
Degee 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 equals sum of all out-degrees

4 Traveling a graph Path/walk/trail Cycle/circuit/tour
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 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

6 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

7 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

8 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 Forms the basis for several other operations

9 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

10 Application: Connected Components
Undirected graph – find connected components Loop through all vertices, when you find one unvisited: Run DFS or BFS to pull out all connected pieces Mark the nodes as visited

11 Flood Fill Basic search over a grid, identifying connected components.
Explore current cell, then look at the 8 (or 4, or in 3D 8/26/etc.) adjacent ones recursively Mark the cell by changing the value in the cell Forms basis for lots of algorithms

12 Bipartite Graph Check Check whether bipartite (2-colorable)
Just run BFS (or DFS), coloring the nodes one of two colors as encountered. If there is ever a conflict, then can’t be bipartite

13 Topological Sort Given constraints about what has to come first (i.e. directed graph), find some ordering that is OK Always start with a node that has in-degree of 0 Can modify DFS to do this After visiting all adjacent nodes, add this node to the end of the list Can modify BFS to do this Add nodes with in-degree 0 to list Pop a node off the list, remove all edges coming out If removing an edge makes another node have in-degree 0, enqueue it.

14 Next Time Articulation Points/Bridges Strongly-Connected Components
Minimum Spanning Tree


Download ppt "Graphs Representation, BFS, DFS"

Similar presentations


Ads by Google