570 likes | 784 Views
Data Structure & Algorithm. 10 – Graph & BFS & DFS JJCAO. Graph Are Not. Graphs. G = (V,E ) V[G] = {1,2,3,4,5,6} |V| = 6 E[G] = {{1,2},{1,5},{2,5},{3,6}} Note: { u,v } = ( u,v ) = ( v,u ) ( u,v ): u ↔ v. Can be very complex. Excellent Tool!. Terminology.
E N D
Data Structure & Algorithm 10 – Graph & BFS & DFS JJCAO
Graphs • G = (V,E) • V[G] = {1,2,3,4,5,6} |V| = 6 • E[G] = {{1,2},{1,5},{2,5},{3,6}} • Note: {u,v} = (u,v) = (v,u) • (u,v): u↔v
Object-OrientedRepresentation • Node: some structure, with all relevant information • Edge: name & pointers to two endpoints
Connected Components • Every node v is connected to itself • if u and v are in the same connected component then v is connected to u and u is connected to v • Connected components form a partition of the nodes and so are disjoint:
Adding Direction • An undirected graph can be transformed into a directed one
Terminology • endpoints of an edge = the vertices it connects • e = (u,v) is incident from u = leaves u incident to v = enters v • u is the tail • v is the head • degreeof v = indegree + outdegree • indegree= # incoming edges • outdegree= # outgoing edges • Paths and cycles are now directed
Simple Graphs Are graphs with no parallel edges, no self loops Properties:
Strong Connectivity • Vertices strongly connected ↔ there is a path from each to the other • Graph Strongly connected↔every2 vertices are strongly connected • Strongly Connected Components = maximal strongly connected subgraphs
Strong Connectivity The strongly connected components of the above graph
Graph Traversals Given: a graph G a source vertex s in V[G] Goal: “visit” all the vertices of G to determine some property: – Is G connected? – Does G contain a cycle? – is there a v u path in G? – What is the shortest path connecting v and u? – Will G disconnect if we remove a single edge? A vertex? – G is the WWW - follow links to retrieve information….
Data Structure for BFS • First In First Out (FIFO) Queue
BFS - Running Time Adjacency list representation:
Shortest Paths Goal: Find a shortest path from s to every other v Idea: - start at s - find all vertices at distance 1 - find all vertices at distance 2
Depth First Search Goal: Visit all vertices of the graph Idea: - start at s - keep going “deeper” into the graph whenever possible
Data Structure for DFS • Last In First Out (LIFO) Stack Rewrite the procedure DFS, using a stack to eliminate recursion
Classification of Edges in DFS Forest Relative to the same DFS tree
DFS - Running Time Adjacency list representation
Applications of DFS In (V+E) time, we can: • Find connected components of G • determine if G has a cycle • determine if removing an edge / vertex disconnects G • determine if G is planar • …
Cycles in Directed Graphs Theorem: DiGraphG has a cycle DFS forest has a back edge Proof: • back edge => cycle: obvious • cycle => back edge: Lemma: A directed graph G is acyclicif and only if a depth-first search of G yields no back edges.
Topological Sort an ordering "<" of V[G] such that if (u, v) E[G] => u < v
Application of Topological Sort Many applications use directed acyclic graphs to indicate precedence among events, such as Professor Bumsteadgets dressed in the morning.
Topological Sort Lemma: G can be topologically sorted <=> G is a DAG (directed acyclic graph) Proof: cycle => G can't be sorted - obvious: no cycle => G can be sorted: we will show an algorithm
A Simple TopSort Algorithm Source = a vertex with indegree= 0
A Simple TopSort Algorithm Source = a vertex with indegree= 0