Download presentation
Presentation is loading. Please wait.
Published byArleen Adams Modified over 9 years ago
1
Nattee Niparnan
2
Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4, 5),(6,7)) 1 2 3 4 5 6 7
3
Term you should already know directed, undirected graph Weighted graph Bipartite graph Tree Spanning tree Path, simple path Circuit, simple circuit Degree
4
Representing a Graph Adjacency Matrix A = |V|x|V| matrix a xy = 1when there is an edge connecting node x and node y a xy = 0otherwise 1 2 3 4 5 01010 10110 01001 11001 00110 12345 1 2 3 4 5
5
Representing a Graph Adjacency List Use a list instead of a matrix For each vertex, we have a linked list of their neighbor 1 2 3 4 5 124 2134...
6
Representing a Graph Incidences Matrix Row represent edge Column represent node 1 2 3 4 5 11000 10010 01010 01100 00101 00011 12345
7
Exploring a Maze
8
Exploring Problem
9
Depth-First-Search procedure explore(G; v) // Input: G = (V;E) is a graph; v V // Output: visited(u) is set to true for all nodes u reachable from v { visited(v) = true previsit(v) for each edge (v,u) E if not visited(u) explore(u) postvisit(v) }
10
Example Explore(A)
11
Extend to Graph Traversal Traversal is walking in the graph We might need to visit each component in the graph Can be done using explore Do “explore” on all non-visited node The result is that we will visit every node What is the difference between just looking into V (the set of vertices?)
12
Graph Traversal using DFS procedure dfs(G) { for all v V visited(v) = false for all v V if not visited(v) explore(v) }
13
Complexity Analysis Each node is visited once Each edge is visited twice Why? O( |V| + |E|)
14
Another Example
16
Connectivity in Undirected Graph
17
Connected Component Problem Input: A graph Output: Marking in every vertices identify the connected component Let it be an array ccnum, indexed by vertices
18
Solution Define global variable cc In previsit() ccnum[v] = cc Before calling each explore cc++
19
Ordering in Visit procedure previsit(v) pre[v] = clock clock = clock + 1 procedure postvisit(v) post[v] = clock clock = clock + 1
20
Ordering in Visit The interval for node u is [pre(u),post(u)] The inverval for u,v is either Contained disjointed Never intersect
21
DFS in Directed Graph
22
Type of Edge in Directed Graph
23
Directed Acyclic Graph (DAG) A directed Graph without a cycle Has “source” A node having only “out” edge Has “sink” A node having only “in” edge How can we detect that a graph is a DAG What should be the property of “source” and “sink” ?
24
Solution A directed graph is acyclic if and only if it has no back edge Sink Having lowest post number Source Having highest post number
25
Linearization of Graph
26
Linearization One possible linearization B,A,D,C,E,F Order of work that can be done w/o violating the causality constraints
27
Topological Sorting Problem
28
Topological Sorting Do DFS List node by post number (descending) 1,12 2,9 3,84,5 6,7 10,11
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.