Stack, queue, 運算式解析 二元樹與走訪 圖的 DFS 與 BFS 拓撲排序演算法 尤拉迴路 Uva 514, 樹狀結構 Day5: 資料結構基礎 07/12 樹 (tree) 是一種特殊的資料結構, 它可以用來描述有分支的結構, 是由一個或一個以上的節點所組成的有限集合,

Similar documents
Microsoft PowerPoint - DS&Algorithm [相容模式]

Microsoft PowerPoint - 資料結構總複習

Microsoft PowerPoint Training-1 (graph theory).pptx

投影片 1

C/C++ - 字符输入输出和字符确认

Open topic Bellman-Ford算法与负环

C/C++ - 函数

<5B BECBB0EDB8AEC1F25D312D34B0AD5FC3E2BCAEBCF6BEF7C0DAB7E F31702E504446>

Tree

Microsoft Word htm

C/C++程序设计 - 字符串与格式化输入/输出

Microsoft Word - ACL chapter02-5ed.docx

C/C++ 语言 - 循环

Microsoft PowerPoint - Application of Classical Trees.pptx

C/C++语言 - 运算符、表达式和语句

C/C++ - 字符串与字符串函数

1

PowerPoint Presentation

TX-NR3030_BAS_Cs_ indd

演算法導入、ソート、データ構造、ハッシュ

Process Data flow Data store External entity 6-10 Context diagram Level 0 diagram Level 1 diagram Level 2 diagram

Chapter 1 Introduction

untitled

Explain each of the following terms. (12%) (a) O(n 2 ) (b) protected in C++ language (c) sparse matrix 7. Write

Microsoft PowerPoint - tree

1

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

CC213

C C

C/C++ - 文件IO

C

Microsoft Word - 1- 封面

ENGG1410-F Tutorial 6

C/C++语言 - 分支结构

2

C++ 程式設計

資料結構與演算法複習試題(出自:全國資訊競賽89, 91、IOI 2002, 2003)

Microsoft Word htm

Microsoft Word - DataStruct-981.doc

Microsoft Word - Final Exam Review Packet.docx

Microsoft PowerPoint - Fig03_Stack.ppt [相容模式]

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

C

提纲 1 2 OS Examples for 3

e bug 0 x=0 y=5/x 0 Return 4 2

Microsoft Word - 第3章.doc

<4D F736F F D20C4A3B0E632A3A8D3EFD1D4CEC4D7D6BCECB2E9B8C4A3A92E646F63>

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

untitled

龍華科技大學數位典藏論文

Microsoft Word - 专论综述1.doc

ebook39-5

PowerPoint Presentation

Computer Architecture

C/C++语言 - C/C++数据

extend

untitled

碩命題橫式

untitled

3 (s05q6) The diagram shows the velocity-time graph for a lift moving between floors in a building. The graph consists of straight line segments. In t

Liao Mei-Yu Professor, Department of Chinese Literature, National Cheng Kung University Abstract Yao Ying was a government official in Taiwan for more

BC04 Module_antenna__ doc

E..

C 1

2/80 2

untitled

Microsoft Word - MSP430 Launchpad 指导书.docx

CC213

LEETCODE leetcode.com 一 个 在 线 编 程 网 站, 收 集 了 IT 公 司 的 面 试 题, 包 括 算 法, 数 据 库 和 shell 算 法 题 支 持 多 种 语 言, 包 括 C, C++, Java, Python 等 2015 年 3 月 份 加 入 了 R

Microsoft Word - 051KK170AP009ZP01資構.docx

ebook39-6

3.1 num = 3 ch = 'C' 2

2015 Chinese FL Written examination

Microsoft Word - 論文封面 修.doc

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

1

untitled

全国计算机技术与软件专业技术资格(水平)考试

Microsoft PowerPoint - Lecture7II.ppt

Chapter12 Derived Classes

壹 前言 一 研究動機 由於我是全國商業類技藝競賽程式設計職種的選手之一, 因此有機會參與程式設計類的研習, 在一次由三重商工林易民老師主講的研習中, 他提到 二元樹拜訪 給定前序 中序轉後序 相當有挑戰性, 當時聽到這個題目就試著解題 林易民老師提到一般傳統解此題的演算法是以 先重建二元樹 再以後

Microsoft PowerPoint - Model Checking a Lazy Concurrent List-Based Set Algorithm.ppt [Compatibility Mode]

A Community Guide to Environmental Health

川 外 250 人, 上 外 222 人, 广 外 209 人, 西 外 195 人, 北 外 168 人, 中 南 大 学 135 人, 西 南 大 学 120 人, 湖 南 大 学 115 人, 天 外 110 人, 大 连 外 国 语 学 院 110 人, 上 海 外 事 学 院 110 人,

Microsoft Word - 11.doc

untitled

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

2009 Japanese First Language Written examination

Microsoft Word - 09.數學 docx

蔡 氏 族 譜 序 2

untitled

2010 Japanese First Language Written examination

C/C++ - 数组与指针

untitled

2009 Korean First Language Written examination

Microsoft Word - A doc

1505.indd

穨1-林聖欽.doc

Transcription:

Stack, queue, 運算式解析 二元樹與走訪 圖的 DFS 與 BFS 拓撲排序演算法 尤拉迴路 Uva 514, 10305 樹狀結構 Day5: 資料結構基礎 07/12 樹 (tree) 是一種特殊的資料結構, 它可以用來描述有分支的結構, 是由一個或一個以上的節點所組成的有限集合, 且具有下列特質 : 存在一個特殊的節點, 稱為樹根 ( root) 其餘的節點分為 n 0 個互斥的集合,T 1, T 2, T 3 T n, 且每個集合稱為子樹 樹由數個節點 (node) 與將節點連接起來的分支 (branch) 所組成 節點分支出來的節點稱為 子節點, 其上層的節點稱為 父節點 樹的最上面節點稱為 根 (root) 底下沒有子節點的節點稱為樹葉 (leaf) 的深度與高度 Depth( 深度 ): 由根到某個節點間所通過的分支數, 稱為該節點的深度 Height( 高度 ): 由根到最深節點的深度, 稱為樹的高度 問 : 1. 求節點 B G E 的深度 2. 求樹的高度 3. 求節點 A( 根 ) 的深度 1

二元樹 ( 樹的各節點分支如在 2 個以下 ( 含 2 個 ), 則稱為二元樹 (binary tree) 二元搜尋樹 ( 二元搜尋樹是一種二元樹, 它可以為空, 若不為空, 則必須要滿足以下條件 : 1. 若左子樹不為空, 則左子樹的鍵值均須要小於樹根的鍵值 2. 若右子樹不為空, 則右子樹的鍵值均須要大於樹根的鍵值 3. 左子樹與右子樹必須也要保持二元搜尋樹 下列何種順序所建造的二元搜尋樹 (Binary Search Tree) 最平衡 (Balanced)? (a) 30,20,50,5,25,41,80 (b) 5,20,25,30,41,50,80 (c) 80,50,41,30,25,20,5 (d) 50,80,41,30,25,20,5 如果依序輸入六筆資料, 下列何者所建立的二元搜尋樹 (Binary Search Tree) 層數最少? (a) 100, 200, 300, 400, 500, 600 (b) 300, 200, 500, 400, 100, 600 (c) 600, 500, 400, 300, 200, 100 (d) 400, 100, 500, 100, 200, 600 二元搜尋樹的追蹤 ( 前序追蹤 (preorder traversal) 中序追蹤 (inorder traversal) 後序追蹤 (postorder traversal) 2

前序追蹤 (preorder traversal) 中左右 1. 顯示節點 2. 巡視左側樹的遞迴呼叫 3. 巡視右側樹的遞迴呼叫 資料顯示順序 : 50 35 25 40 36 41 60 中序追蹤 (inorder traversal) 左中右 1. 巡視左側樹的遞迴呼叫 2. 顯示節點 3. 巡視右側樹的遞迴呼叫 資料顯示順序 : 25 35 36 40 41 50 60 後序追蹤 (postorder traversal) 左右中 1. 巡視左側樹的遞迴呼叫 2. 巡視右側樹的遞迴呼叫 3. 顯示節點資料顯示順序 : 25 36 41 40 35 60 50 Inorder: Preorder: Postorder: 計算式樹 有一計算式樹, 三種 traversal 方式分別如下 : 前序 :-*ab/+cde 中序 :a*b-c+d/e 後序 :ab*cd+e/- 請畫出此樹 3

圖形 ( 圖形 (Graph) 是指以邊 (Edge) 將節點 (node, Vertex) 連接起來的物件 G=(V, E) V = vertex set E = edge set 圖形表示法 : Adjacency list Adjacency matrix 無向圖 ( ) 表示法 有向圖 ( ) 表示法 4

圖形的搜尋 Breadth-first search (BFS) Depth-first search (DFS) Topological sort Strongly connected components 5

#include <string.h> #define N 7 int visited[n]; // visited array int graph[n][n] = 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; int front = 0; int rear = 0; int q[n] = 0 ; void bfs(int v); int main() // make all vertex unvisited memset(visited, 0, sizeof(visited)); // run bfs from 0th vertex bfs(0); return 0; void bfs(int v) visited[v] = 1; // make vertex v visited // enqueue vertex v q[rear] = v; // insert at rear rear++; // increment rear while (rear!= front) // condition for empty queue // dequeue int u = q[front]; printf("%d ", u); front++; // check adjacent nodes from u for (int i = 0; i < N; i++) // if there is adjacent vertex enqueue it if (!visited[i] && graph[u][i]) q[rear] = i; rear++; visited[i] = 1; printf("\n"); 畫出這個圖 : 6

BFS: BFS: BFS: 思考 : 依何種方式走訪儲存資料? n number of nodes Initialize visited[ ] to false (0) for(i=0;i<n;i++) visited[i] = 0; void DFS(vertex i) [DFS starting from i] visited[i]=1; for each w adjacent to i if(!visited[w]) DFS(w); 7

#include<stdio.h> void DFS(int); int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10] void main() int i,j; scanf("%d",&n); //read the adjecency matrix for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&g[i][j]); //visited is initialized to zero for(i=0;i<n;i++) visited[i]=0; DFS(0); void DFS(int i) int j; printf("\n%d",i); visited[i]=1; for(j=0;j<n;j++) if(!visited[j]&&g[i][j]==1) DFS(j); DFS: 8

油田 ( 求連通塊 ) Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors. Given a diagram of Farmer John's field, determine how many ponds he has. Input * Line 1: Two space-separated integers: N and M * Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them. Output * Line 1: The number of ponds in Farmer John's field. Sample Input 10 12 W...WW..WWW...WWW...WW...WW....WW....W....W...W...W.W...WW. W.W.W...W..W.W...W...W...W. Sample Output 3 #include<cstdio> #include<cstring> const int maxn=1000+5; char tu[maxn][maxn]; // 輸入圖的陣列 int m,n,idx[maxn][maxn]; // 標記陣列 解析 : 一般用 DFS 找連通塊 從每個 W 開始出發, 遞迴走訪周圍的 W 格子 每次存取一個格子, 就寫一個 連通分量編號 idx 這樣就可以在存取前檢查它是否已有編號 避免同一個格式存取多次 用雙重迴圈找目前格子相鄰 8 個格子 floodfill void dfs(int r,int c,int id) if(r<0 r>=m c<0 c>=n) return; if(idx[r][c]>0 tu[r][c]!='w') return; idx[r][c]=id; for(int dr=-1; dr<=1; dr++) for(int dc=-1; dc<=1; dc++) if(dr!=0 dc!=0) dfs(r+dr,c+dc,id); int main() int i,j; while(scanf("%d%d",&m,&n)==2&&m&&n) for(i =0; i<m; i++) scanf("%s",tu[i]); memset(idx,0,sizeof(idx)); int q=0; // 尋找周圍八塊 9

for(i=0; i<m; i++) for(j=0; j<n; j++) if(idx[i][j]==0&&tu[i][j]=='w') dfs(i,j,++q); printf("%d\n",q); return 0; 拓樸排序 ( 拓樸排序是指以某種規則將一有向圖形連接的節點排列成一列的情形 方法不是唯一 Ans: 一筆畫 void euler(int u) for (int v=0; v<n; v++) if (G[u][v] &&!vis[u][v]) vis[u][v] = vis[v][u] = 1; euler(v); printf( %d %d\n, u, v); 1736 年, 尤拉發表了他的 一筆劃定理, 大致如下 : 一個圖形要能一筆劃完成必須符合兩個狀況 : 1. 圖形是封閉連通的 ; 2. 圖形中的奇點個數為 0 或 2 10

最易理解, 也最易以手算的方法 Kruskal's algorithm: sort the edges of G in increasing order by length keep a subgraph S of G, initially empty for each edge e in sorted order if the endpoints of e are disconnected in S add e to S return S 這是一個 Greedy method ( 貪心演算法 ) 11

A 24 5 6 10 C 12 D 18 F A 2 B 3 7 5 D 8 B 30 25 15 E 21 4 C 6 E 最短路徑 12