Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
1,47 MB
Nội dung
Data Structure and Algorithms [CO2003] Graph Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Faculty of Computer Science and Engineering Hochiminh city University of Technology Contents Graph definition Depth-First Search Breath-First Search Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 33 Outcomes • L.O.7.1 – Depict the following concepts: connected graph, disconnected graph, direct/undirect graph, etc • L.O.7.2 – Depict storage structures for graph and describe graph using pseudocode in the cases of using adjacency matric and adjacency list • L.O.7.3 – List necessary methods supplied for graph, and describe them using pseudocode • L.O.7.4 – Depict basic traversal methods step-by-step (depth first and bread-first) • L.O.7.5 – Implement storage structures for graphs using C/C++ • L.O.7.6 – Implement basic traversal methods using C/C++ • L.O.7.7 – Depict the working steps of Dijkstra and Prim step-by-step Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 33 Graph definition Graph Definition A graph G is defined by • a set of vertices V (G) • a set of edges (or arcs) E(G) ⊆ [V (G)]2 Graph captures/represents the abtract relations between objets (vertices) Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 33 Graph Definition A graph G is defined by • a set of vertices V (G) • a set of edges (or arcs) E(G) ⊆ [V (G)]2 Graph captures/represents the abtract relations between objets (vertices) Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 33 Graph Definition A graph G is defined by • a set of vertices V (G) • a set of edges (or arcs) E(G) ⊆ [V (G)]2 Graph captures/represents the abtract relations between objets (vertices) 2 4 Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 33 Adjacency matrices Give the graph defined by the following adjacency matrices A B C D E A B C D E A 0 1 0 0 1 0 1 1 0 B C D E A B C D E 1 1 Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 0 1 0 1 0 0 0 / 33 Incidence matrice Define incidence matrice of the following graph F B C A E D G Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 33 Graph G = (V, E) with adjacency lists Adj • class Vertex • Integer id, name, color, • end • class AdjList • vertex [] l, • Integer nVertice (number of adjacent vertice) • end • class Graph • VerticeList [] g; • • end Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 33 Graph G = (V, E) with adjacency lists/matrix Adj DFSVisit(Adj, u) DFS(G) color[u] ← G (“Gray”) for each vertice u of V color[u] ← W (“White”) p[u] ← null time ← d[u] ← time ← time + for each v of Adj[u] if color[v] = W then p[v] ← u DFSVisit(Adj, v) for each vertex u of V if color[u] = W then DFSVisit(Adj, u) color[u] ← B (“Black”) f[u] ← time ← time + Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 28 / 33 Exerxise Implement DFS algorithm in C/C++ by using adjacency matrice/lists and incident matrice Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 29 / 33 Exerxise Implement DFS algorithm in C/C++ by using adjacency matrice/lists and incident matrice Calculate complexity of each implementation and determine the best implementation Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 29 / 33 Application Find vertice set accessible from a vertex s Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 33 Application Find vertice set accessible from a vertex s Determine topological order in an acyclic graph Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 33 Application Find vertice set accessible from a vertex s Determine topological order in an acyclic graph Calculate number of connected components in a directed graph Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 33 Application Find vertice set accessible from a vertex s Determine topological order in an acyclic graph Calculate number of connected components in a directed graph Determine whether there exists a cycle in graph Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 33 Application Find vertice set accessible from a vertex s Determine topological order in an acyclic graph Calculate number of connected components in a directed graph Determine whether there exists a cycle in graph Verify whether the graph is bipartite Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 33 Application Find vertice set accessible from a vertex s Determine topological order in an acyclic graph Calculate number of connected components in a directed graph Determine whether there exists a cycle in graph Verify whether the graph is bipartite Identify articulation points Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 33 Existence of a cycle in a graph DFS(G) for each vertex u of V color[u] ← W (“White”) parent[u] ← null time ← for each vertex u of V if color[u] = W then DFSVisit(Adj, u) Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 31 / 33 Existence of a cycle in a graph DFSVisit(Adj, u) color[u] ← G (“Gray”) DFS(G) d[u] ← temps ← temps + 1 for each vertex u of V color[u] ← W (“White”) parent[u] ← null time ← for each vertex v of Adj[u] if couleur[v] = W then p[v] ← u DFSVisit(Adj, v) for each vertex u of V if color[u] = W then DFSVisit(Adj, u) if (color[v] = G) & (p[u] 6= v) then There exists a cycle here! color[u] ← B (“Black”) 10 f[u] ← time ← time + Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 31 / 33 Breath-First Search Graph G = (V, E) with adjacency lists/matrix Adj BFS(G) create a queue Q for each vertex u of V color[u] ← W (“White”) p[u] ← null time ← for each vertex u of V if color[u] = W then Q.EnQueue(u) color[u] ← G (“Gray”) 10 d[u] ← time ← time + 11 BFSVisit(Adj, Q) Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 32 / 33 Graph G = (V, E) with adjacency lists/matrix Adj BFSVisit(Adj, Q) BFS(G) create a queue Q while (Q is not null) for each vertex u of V u ← Q.front() color[u] ← W (“White”) for each v of Adj[u] p[u] ← null if color[v] = W then time ← color[v] ← G; for each vertex u of V p[v] ← u d[v] ← time ← time + if color[u] = W then Q.EnQueue(u) color[u] ← G (“Gray”) Q.EnQueue(v) Q.DeQueue() 10 d[u] ← time ← time + 10 color[u] ← B (“Black”) 11 BFSVisit(Adj, Q) 11 f[u] ← time ← time + Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 32 / 33 Graph G = (V, E) with adjacency lists/matrix Adj is bipartite? BFSVisit(Adj, Q) BFS(G) create a queue Q while (Q is not null) for each vertex u of V u ← Q.front() color[u] ← W (“White”) for each v of Adj[u] p[u] ← null if color[v] = W then time ← color[v] ← G; for each vertex u of V p[v] ← u if color[u] = W then if (X[u]=0) then X[v]=1 else X[v]=0 Q.EnQueue(u) d[v] ← time ← time + color[u] ← G (“Gray”) Q.DeQueue() X[u] ← 10 11 d[u] ← time ← time + 11 Q.EnQueue(v) 12 if (BFSVisit(Adj, Q) = ’N’ then 12 color[u] ← B (“Black”) 13 f[u] ← time ← time + 10 13 return ’N’ else if (X[u]=X[v]) return ’N’ Lecturer: Nguyen Huynh-Tuong, PhD & Duc-Dung Nguyen, PhD Contact: {htnguyen;nddung}@hcmut.edu.vn Data Structure and Algorithms [CO2003] 33 / 33 ... Depict the following concepts: connected graph, disconnected graph, direct/undirect graph, etc • L.O.7.2 – Depict storage structures for graph and describe graph using pseudocode in the cases of... Structure and Algorithms [CO2003] / 33 Graph definition Graph Definition A graph G is defined by • a set of vertices V (G) • a set of edges (or arcs) E(G) ⊆ [V (G)]2 Graph captures/represents the abtract... topological order in an acyclic graph Calculate number of connected components in a directed graph Determine whether there exists a cycle in graph Verify whether the graph is bipartite Lecturer: