6.1.3 Graph representation.

Slides:



Advertisements
Similar presentations
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Advertisements

CHAPTER 6 GRAPHS All the programs in this file are selected from
Chapter 8, Part I Graph Algorithms.
1 Representing Graphs. 2 Adjacency Matrix Suppose we have a graph G with n nodes. The adjacency matrix is the n x n matrix A=[a ij ] with: a ij = 1 if.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
CS Data Structures Chapter 6 Graphs.
C. Y. Tang and J. S. Roger Jang
IS 2610: Data Structures Graph April 5, 2004.
Copyright Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Binary Search Trees Berlin Chen 陳柏琳 台灣師範大學資工系 副教授 參考資料來源:
CHAPTER 6 GRAPHS All the programs in this file are selected from
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
6.1.3 Graph representation.
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.
Graphs 2015, Fall Pusan National University Ki-Joune Li.
Graphs and Paths : Chapter 15 Saurav Karmakar
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Graph Search Applications, Minimum Spanning Tree
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Graphs A New Data Structure
Graphs Chapter 20.
Chapter 28 Graphs and Applications
Instructor : Prof. Jyh-Shing Roger Jang
Graphs Representation, BFS, DFS
Data Structures 13th Week
Graphs -An abstract way of representing connectivity using nodes (also called vertices) and edges vertices edges directed undirected - weighted.
Csc 2720 Instructor: Zhuojun Duan
Graph Search Lecture 17 CS 2110 Fall 2017.
Unit 3 Graphs.
CC 215 Data Structures Graph Searching
CS120 Graphs.
CHAPTER 6 GRAPHS All the programs in this file are selected from
Graph Algorithm.
Graph Search Lecture 17 CS 2110 Spring 2018.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Lecture 14 CSE 331 Sep 30, 2011.
Can you get there from here?
2018, Fall Pusan National University Ki-Joune Li
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Depth-First Search D B A C E Depth-First Search Depth-First Search
Chapter 11 Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graphs Part 2 Adjacency Matrix
Lecture 13 CSE 331 Sep 27, 2017.
2017, Fall Pusan National University Ki-Joune Li
Graphs – Adjacency Matrix
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Subgraphs, Connected Components, Spanning Trees
Lecture 14 CSE 331 Oct 3, 2012.
Data Structures and Programming Techniques
Algorithms: Design and Analysis
CHAPTER 6 GRAPHS All the programs in this file are selected from
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Analysis and design of algorithm
Lecture 13 CSE 331 Sep 28, 2016.
6.1.3 Graph representation.
For Friday Read chapter 9, sections 2-3 No homework
Presentation transcript:

6.1.3 Graph representation

常見的表示法 Adjacency matrices Adjacency lists Adjacency multilists 1 2 3 1 2 3 G1 常見的表示法 Adjacency matrices Adjacency lists Adjacency multilists 2 1 G3 1 2 3 4 6 5 7 G4

Adjacency matrix n*n 陣列 n*(n-1),即 O(n2) If the matrix is sparse ? G3 n*n 陣列 n*(n-1),即 O(n2) If the matrix is sparse ? 大部分元素是0 e << (n2/2) 1 2 3 G1

Adjacency lists n個linked list 2 1 G3 1 2 #define MAX_VERTICES 50 G3 1 2 n個linked list #define MAX_VERTICES 50 typedef struct node *node_ptr; typedef struct node { int vertex; node_ptr link; } node; node_ptr graph[MAX_VERTICES]; int n = 0; /* number of nodes */ 3 1 2 1 2 3 G1

Adjacency lists, by array 2 1 G3 1 2

Adjacency multilists 1 2 3 G1 m vertex1 vertex2 list1 list2 N0 1 2 3 G1 Adjacency multilists m vertex1 vertex2 list1 list2 N0 0 1 N1 N3 N1 0 2 N2 N3 typedef struct edge *edge_ptr; Typedef struct edge { int marked; int vertex1; int vertex2; edge_ptr path1; edge_ptr path2; } edge; edge_ptr graph[MAX_VERTICES]; N2 0 3 NIL N4 N3 1 2 N4 N5 N4 1 3 NIL N5 N5 2 3 NIL NIL

Weighted edges Cost Weight field Network

6.2 Elementary graph operations

Outlines Operations similar to tree traversals Depth-First Search (DFS) Breadth-First Search (BFS) Is it a connected graph? Spanning trees Biconnected components

Depth-First Search Adjacency list: O(e) Adjacency Mtx: O(n2) 例如:老鼠走迷宮 int visited[MAX_VERTICES]; void dfs(int v) { node_ptr w; visited[v] = TRUE; printf(“%5d”, v); for (w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); }

Breadth-First Search 例如:地毯式搜索 void bfs(int v) { node_ptr w; queue_ptr front, rear; front=rear=NULL; printf(“%5d”,v); visited[v]=TRUE; addq(&front, &rear, v); while(front) { v = deleteq(&front); for(w=graph[v]; w; w=w->link) if(!visited[w->vertex]) { printf(“%5d”, w->vertex); addq(&front, &rear, w->vertex); visited[w->vertex] = TRUE; } typedef struct queue *queue_ptr; typedef struct queue { int vertex; queue_ptr link; }; void addq(queue_ptr *, queue_ptr *, int); Int deleteq(queue_ptr); 例如:地毯式搜索

Connected component Is it a connected graph? BFS(v) or DFS(v) Find out connected component void connected(void){ int i; for (i=0;i<n;i++){ if(!visited[i]){ dfs(i); printf(“\n”); }

Spanning Tree A spanning tree is a minimal subgraph G’, such that V(G’)=V(G) and G’ is connected Weight and MST 3 1 2 1 2 3 G1 1 2 1 2 3 3 DFS(0) BFS(0)

Biconnected components Definition: Articulation point (關節點) 原文請參閱課本 如果將vertex v以及連接的所有edge去除,產生 graph G’, G’至少有兩個connected component, 則v稱為 articulation point Definition: Biconnected graph 定義為無Articulation point的connected graph Definition: Biconnected component Graph G中的Biconnected component H, 為G中最大的biconnected subgraph; 最大是指G中沒有其他subgraph是biconnected且包含入H

A connected graph and its biconnected components 1 2 3 4 5 7 6 8 9 1 7 8 7 9 1 2 3 4 5 7 6 3 5 為何沒有任一個 點/邊 可能存在於兩個或多個biconnected component中?

DFS spanning tree of the graph in 6.19(a) Root at 3 Back edge and cross edge 1 2 3 4 5 7 6 8 9 10 1 2 3 4 5 7 6 8 9 10

dfn() and low() Observation low(u): u及後代,其back edge可達vertex之最小dfn() 若root有兩個以上child, 則為articulation point 若vertex u有任一child w, 使得w及w後代無法透過back edge到u的祖先, 則為 articulation point low(u): u及後代,其back edge可達vertex之最小dfn() low(u) = min{ dfn(u), min{low(w)|w是u的child}, min{dfn(w)|(u,w)是back edge}}

A example: dfs() and low() 1 2 3 4 5 7 6 8 9 10 1 2 3 4 5 7 6 8 9 10 請自行Trace Biconnected() Hint: 將Unvisited edge跟back edge送入Stack, 到Articulation Point 再一次輸出