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

Similar documents
Microsoft PowerPoint - 資料結構總複習

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

Microsoft PowerPoint Training-1 (graph theory).pptx

投影片 1

Microsoft Word htm

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

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

Microsoft Word - DataStruct-981.doc

Open topic Bellman-Ford算法与负环

Microsoft Word htm

Tree

Chapter 1 Introduction

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

Microsoft Word - 051KK170AP009ZP01資構.docx

Microsoft PowerPoint - tree

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

PowerPoint Presentation

untitled

1

1

2

不 知 肉 味 的 用 法 相 同? (A) 長 煙 一 空, 皓 月 千 里 (B) 五 臟 六 腑 裡, 像 熨 斗 熨 過, 無 一 處 不 伏 貼 (C) 兩 片 頑 鐵, 到 他 手 裡, 便 有 了 五 音 十 二 律 似 的 (D) 吾 觀 三 代 以 下, 世 衰 道 微 12. 文

1

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("%


(CIP) /. :, ISBN T P CIP (2005) : : 127 : : : : : 787 mm mm 1

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

資料結構之C語言重點複習

TX-NR3030_BAS_Cs_ indd

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

<4D F736F F D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF>

204 */ InitiateStack s ; /* s */ i = n; t = p = new node; /* */ p->data = postorder[i]; while i > q = new node; if parent[i - ] == postorder[i] S,T S

立 志 于 打 造 最 贴 近 考 生 实 际 的 辅 导 书 计 算 机 考 研 之 数 据 结 构 高 分 笔 记 率 辉 编 著 周 伟 张 浩 审 核 讨 论 群 :

Fuzzy Highlight.ppt

Microsoft Word - 专论综述1.doc

Microsoft Word - data_mid1611_and_sol.docx

2. 佔 中 對 香 港 帶 來 以 下 影 響 : 正 面 影 響 - 喚 起 市 民 對 人 權 及 ( 專 制 ) 管 治 的 關 注 和 討 論 o 香 港 市 民 總 不 能 一 味 認 命, 接 受 以 後 受 制 於 中 央, 沒 有 機 會 選 出 心 中 的 理 想 特 首 o 一

Linked Lists

威 福 髮 藝 店 桃 園 市 蘆 竹 區 中 山 里 福 祿 一 街 48 號 地 下 一 樓 50,000 獨 資 李 依 純 105/04/06 府 經 登 字 第 號 宏 品 餐 飲 桃 園 市 桃 園 區 信 光 里 民

08np.ppt

C 1

Microsoft Word - 103高考-資料結構

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

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

ENGG1410-F Tutorial 6

Microsoft Word - ACL chapter00a-1ed .doc

PowerPoint Presentation

CC213

Turing Machine [1] n n n findmin (a 1, a 2,, a n ) 1. result a 1 2. index 2 3. result min (result, aindex) 4. index index go to step 3 till (in

软件测试(TA07)第一学期考试

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

PowerPoint 演示文稿

untitled

第一章 出口退税制改革的内容

Microsoft PowerPoint - Lecture7II.ppt

試題評析

Microsoft PowerPoint - STU_EC_Ch08.ppt

PowerPoint Presentation

What is Version Control? What is Git?

系 ( 類 ) 別部別及年級 科一屹資訊工程系 日間部 進修部 B 年級 資料結構 若一演算法的執行時間不因輸入量的多寡而有所變動 何者? (A) 0(1) (B) 0(e) (C)O(Ioge) (B ) 以上皆非 總分 : 200 分 第 2 頁共 5 頁 亦即其執行時間因定不變者係為下列 16

MailCloud信箱代管定價表

AL-M200 Series

ebook39-5

當 地 情 形 還 不 熟 悉 4 得 勝 的 歡 似 虎 : 形 容 因 勝 利 而 得 意 忘 形 5 不 吃 無 工 之 食 : 比 喻 人 不 能 無 緣 無 故 接 受 優 待 或 贈 與 4. 請 根 據 文 意, 在 中 填 入 正 確 的 成 語 代 號 ( 甲 ) 優 游 自 在

什么是函数式编程?


4. 每 组 学 生 将 写 有 习 语 和 含 义 的 两 组 卡 片 分 别 洗 牌, 将 顺 序 打 乱, 然 后 将 两 组 卡 片 反 面 朝 上 置 于 课 桌 上 5. 学 生 依 次 从 两 组 卡 片 中 各 抽 取 一 张, 展 示 给 小 组 成 员, 并 大 声 朗 读 卡

國立中山大學學位論文典藏.PDF

ebook20-2

K301Q-D VRT中英文说明书141009

提纲 1 2 OS Examples for 3

<4D F736F F F696E74202D20B5DAD2BBD5C228B4F2D3A1B0E6292E BBCE6C8DDC4A3CABD5D>

投影片 1

試題評析

% 6.% 9.6% % 7.% 1.8% % 68.7% 14.5% : 15.8% 57.9% 4.7%

プリント

投影片 1

旅 句 良 年 理 了 來 不 不 更 更 說 識 更 樓 歷 練 靈 旅 論 不 了 契 諒 老 老 老 不 勵 老 不 良 論 漏 不 老 老 不 勵 不 了 了 老 論 利 行 老 見 不 見 更 老 玲 歷 老 料 理

A Community Guide to Environmental Health

Microsoft PowerPoint - Lecture10.ppt

VASP应用运行优化

本章內容 3-1 串列的定義 3-2 用陣列直接儲存串列 循序配置串列 3-3 串列加上鏈結 鏈結配置串列 3-4 用結構體陣列實作鏈結串列 3-5 指標與結構體 3-6 動態配置節點實作鏈結串列 3-7 鏈結串列的其他運算 3-8 環狀鏈結串列 3-9 雙向鏈結串列 *3-10 鏈結串列的應用 2

试卷格式

coverage2.ppt

0 0 = 1 0 = 0 1 = = 1 1 = 0 0 = 1

恩 典 课 堂 教 学 概 览 课 堂 环 节 持 续 时 间 活 动 所 需 材 料 1 欢 迎 持 续 在 门 口 欢 迎 学 生, 聆 听 他 们 分 享 本 周 开 心 或 烦 恼 的 事 预 备 活 动 <10 分 钟 A 猜 猜 是 谁 B 上 帝 的 礼 物 无 孩 子 们 的 儿 时

团 契 就 体 力 来 说, 参 孙 乃 是 地 上 极 强 壮 的 人 ; 但 在 自 制 忠 贞 和 坚 稳 上, 他 却 是 人 间 最 软 弱 的 了 先 祖 与 先 知 第 页 教 室 布 置 见 第 一 课 课 堂 教 学 概 览 课 堂 环 节 持 续 时 间 活 动

Microsoft PowerPoint - CH 04 Techniques of Circuit Analysis

!"# $% & $%%% ( ")*+,-./00-(11.-. $%! $ " # $ % & ( - ) +%23!"# $%%% %,.%,!" $%.! 1.% & /$ 3(,. ( /0% $%%% ( $%%% ( 3 5 /6%%%! ")*+,-./00-(11

!! "!! "! "!! "! "! "!!#$% & ()*+, -./!000$ 1-2$##0! 3

ebook14-4


CC213

Microsoft PowerPoint - Aqua-Sim.pptx

汇集全球21位医生的经验和智慧,总结出最实用的专业建议,这些都是最值得你牢记的健康提醒

1 行 业 发 展 不 平 衡 我 国 房 地 产 中 介 服 务 业 起 步 较 晚, 专 业 分 工 程 度 和 国 外 发 达 国 家 相 比 还 有 很 大 差 距 房 地 产 中 介 服 务 行 业 的 发 展 水 平 与 房 地 产 开 发 行 业 的 市 场 化 水 平 密 切 相 关

Introduction to Hamilton-Jacobi Equations and Periodic Homogenization

Journal of Hangzhou Normal University Humanities and Social Sciences No. 6 Nov ! / % & 2 PP P. 9

國立勤益科技大學 101 學年度研究所碩士班招生筆試試題卷所別 : 工業工程與管理系組別 : 資訊系統組科目 : 計算機概論准考證號碼 : ( 考生自填 ) 考生注意事項 : 一 考試時間 100 分鐘 選擇題 :<20 題, 每題 5 分, 共 100 分 > 1. CPU 的速度為 5 MIPS

Transcription:

資料結構與演算法 陳怡芬

什麼是 Data structure? 將資料群組織起來的抽象資料型態, 稱為資料結構

典型的資料結構 資料表格 (Table) 堆疊 (stack) 佇列 (queue) 串列 (list) 樹 (tree) 圖形 (graph) table, stack, queue: 可用陣列表現出來 List, tree, graph: 適合用指標表現出來

堆疊 (Stack) 將資料依序從堆疊下面儲存起來, 並視需要從堆疊的上面將資料取出的方式之資料結構, 稱為堆疊 Push Out top top top top E D A Stack top top E D A Stack

堆疊 (Stack) 特性 : 後進先出 (LIFO) LIFO:last in first out 動作 :push : 儲存資料進堆疊 pop : 將資料從堆疊中取出

Push 的演算法 : int top=0; //top 初值為 0 push(n) { if (top<maxsize){ stack[top]=n; top++; return 0; } else return -1; }

Pop 的演算法 : pop( ) { if (top>0){ top--; k=stack[top]; return k; } else return -1; }

佇列 (queue) 處理資料的方式先進先出 FIFO:First In First Out out queue[0] queue[2] queue[n-1] in queue[1] head tail 佇列 (queue)

佇列 (queue) out queue[0] queue[2] queue[n-1] in queue[1] head tail 現設有 queue[0] 至 queue[n-1] 共 n 個元素的陣列, 表示佇列開頭的指標設為 head, 表示佇列尾端的指標設為 tail 取出資料 : 在 head 進行 head=head+1 儲存資料 : 在 tail 位置進行 tail=tail+1; 佇列初始狀態 :head=tail=0 佇列為空 : 當 head=tail 時 佇列為滿 :tail+1 為 n 時

佇列 (queue) out queue[0] queue[2] queue[n-1] in queue[1] head tail 解決佇列使用一次就無法使用的問題 : 當 head=tail=n 時 將陣列尾端 queue[n-1] 與開頭 queue[0] 連結起來成為一個環形佇列

鏈結串列 (Linked List) 鏈結串列是一種有順序的串列 data Link(to next node) node address of first node ptr->data ptr->link ptr

Linked List 的優點 它比循序的串列 (sequential list, 如陣列 ) 更容易做任意資料的 插入 (insertions) 與 刪除 (deletions) 在 mat 和 cat 之間加入 sat : 1. Get a node that is currently unused; let its address be paddr. 2. Set the data field of this node to mat. 3. Set paddr s link field to point to the address found in the link field of the node containing cat. 4. Set the link field of the node containing cat to point to paddr.

以結構定義一個 Link List 必要的宣告如下 : typedef struct list_node *list_pointer; typedef struct list_node { char data[4]; list_pointer link; }; list_pointer ptr = NULL;

建立一個新節點 (node) 產生一個新的 node ptr = (list_pointer) malloc(sizeof(list_node)); // 配置一個指標空間 address of first node ptr->data ptr->link ptr 把字串資料 bat 放入 list 中 strcpy(ptr->data, bat ); ptr->link = NULL; address of first node ptr->data ptr->link ptr b a t \0 NULL

Create a two-node list typedef struct list_node *list_pointer; typedef struct list_node { int data; list_pointer link; }; list_pointer ptr = NULL; NULL ptr list_pointer create2() { ptr list_pointer first, second; first = (list_pointer) malloc(sizeof(list_node)); second = (list_pointer) malloc(sizeof(list_node)); second->link = NULL; second->data = 20; first->data = 10; first->link = second; return first; } 10 20 NULL

刪除節點 (Deletion )from a list ptr void delete(list_pointer *ptr, list_pointer trail, list_pointer node) { /* ptr may change, pass in the address of ptr */ /* trail is the preceding node, ptr is the head of the list */ if (trail) trail->link = node->link; else *ptr = (*ptr)->link; free(node); } node trail = NULL ptr 10 50 20 NULL 50 20 NULL delete(&ptr, NULL, ptr) ptr trail node ptr 10 50 20 NULL 10 20 NULL delete(&ptr, ptr, ptr->link)

Linked List 的應用 多項式 (Polynomials) 表示 e m 1 A( x) a x m 1 a 0 x e 0 typedef struct poly_node *poly_pointer; typedef struct poly_node { int coef; int expon; poly_pointer link; }; poly_pointer a, b, d; coef expon link

環形 linked lists If the link field of the last node points to the first node in the list, all the nodes of a polynomial can be freed more efficiently. Circular 表示方法 : ptr 3x 14 2x 8 1 ptr 3 14 2 8 1 0

樹 (Tree) Tree 是資料結構中最重要且常用的單位 將各種須分類的事物 ( 如家譜或公司的組織圖 ) 有階層的顯示出來的方式, 正如一棵樹一樣, 由根而枝而樹葉地展開

樹 (Tree) 樹由數個節點 (node) 與將節點連接起來的分支 (branch) 所組成 節點分支出來的節點稱為 子節點, 其上層的節點稱為 父節點 樹的最上面節點稱為 根 (root) 底下沒有子節點的節點稱為樹葉 (leaf)

樹 (Tree) 樹的各節點分支如在 2 個以下 ( 含 2 個 ), 則稱為二元樹 (binary tree) B A 分支 (branch) C 節點 B A C D E D E F G H F H 二元樹 (binary tree)

樹 (Tree) Depth( 深度 ): 由根到某個節點間所通過的分支數, 稱為該節點的深度 Height( 高度 ): 由根到最深節點的深度, 稱為樹的高度 問 : 1. 節點 B G E 的深度分別為? 2. 樹的高度為? 3. 節點 A( 根 ) 的深度為? B F A D G 分支 (branch) C H 節點 E

二元搜尋樹 (binary search tree) 二元樹之中, 各個節點的資料可以相互比較, 父節點與子節點的關係按某種規則 ( 大 小 ) 排列的樹稱為二元搜尋樹 (binary search tree)

二元搜尋樹的追蹤 (traversal) 以一定的步驟巡視樹的所有節點, 稱為樹的追蹤 (traversal) 也有人稱為尋訪 50 35 60 遞迴呼叫 25 40 從遞迴返回 36 41 樹的追蹤

二元搜尋樹的追蹤 (traversal) 樹的追蹤過程中, 巡視過的節點顯示處理 方式分為 : 前序追蹤 (preorder traversal) 中序追蹤 (inorder traversal) 後序追蹤 (postorder traversal) 50 35 60 25 40 36 41 樹的追蹤

前序追蹤 (preorder traversal) 中左右 1. 顯示節點 2. 巡視左側樹的遞迴呼叫 3. 巡視右側樹的遞迴呼叫 資料顯示順序 : 50 35 25 40 36 41 60 25 50 35 60 40 36 41 樹的追蹤

中序追蹤 (inorder traversal) 左中右 1. 巡視左側樹的遞迴呼叫 2. 顯示節點 3. 巡視右側樹的遞迴呼叫 資料顯示順序 : 25 35 36 40 41 50 60 25 50 35 60 40 36 41 樹的追蹤

後序追蹤 (postorder traversal) 左右中 1. 巡視左側樹的遞迴呼叫 2. 巡視右側樹的遞迴呼叫 3. 顯示節點 資料顯示順序 : 25 36 41 40 35 60 50 25 50 35 60 40 36 41 樹的追蹤

計算式樹 有一計算式樹, 三種 traversal 方式分別如下 : 前序 :-*ab+/cde 中序 :a*b-c+d/e 後序 :ab*cd+e/- 請畫出此樹

計算式樹的計算 以後序追蹤計算式樹, 請寫下結果 - * / 5 3 + 2 6 4

計算式樹的計算 以後序追蹤計算式樹, 請寫下結果 - * / 5 3 + 2 6 4

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

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

無向圖 (undirected Graph) 表示法

有向圖 (directed Graph) 表示法

Breadth-First Search (BFS)

Depth-first search (DFS)

拓樸排序 (Topological sort) 拓樸排序是指以某種規則將一有向圖形連接的節點排列成一列的情形 方法不是唯一 1 2 4 5 6 8 3 7

Euler 的一筆畫 1736 年, 尤拉發表了他的 一筆劃定理, 大致如下 : 一個圖形要能一筆劃完成必須符合兩個狀況 : 1. 圖形是封閉連通的 ; 2. 圖形中的奇點個數為 0 或 2

Spanning Tree( 展開樹 ) A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees; for instance the complete graph on four vertices

Minimum spanning tree The weight of a tree is just the sum of weights of its edges. Lemma: Let X be any subset of the vertices of G, and let edge e be the smallest edge connecting X to G-X. Then e is part of the minimum spanning tree.

Kruskal's algorithm 最易理解, 也最易以手算的方法 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 ( 貪心演算法 )

那個邊 (edge) 存在於下圖的最小成本生成樹 (minimum-cost spanning trees) 中? A 5 D Sort: 24 6 10 C 12 18 F 5,6,10,12,15,18,21, 24,25,30 30 15 21 B 25 (a)ab (b)cd (c)ce (d)ef E A 24 5 6 10 C 30 15 12 D 18 21 F B 25 E Minimum cost = 5+6+12+18+24 = 65

Minimum cost spanning tree 下圖中的最小成本擴張樹 (Minimum cost spanning tree) 的成本為 (a)17 (b)20 (c)22 (d)14 A 2 4 B 3 C 7 6 5 D E 8

最短路徑 (Shortest Path)

最短路徑 (Shortest Path)

最短路徑 (Shortest Path)

最短路徑 (Shortest Path)

排序 (sort) 直接選擇法 ( 基本選擇法 )O(n 2 ) 泡泡排序法 ( 基本交換法 )O(n 2 ) 快速排序法 ( 改良交換法 )O(nlog 2 n) Heap 排序 ( 改良選擇法 )O(nlog 2 n) 合併排序 (Merge sort) O(nlog 2 n) Shell 排序 ( 改良插入法 )O(n 1.2 )

Merge Sort Definition: A sort algorithm that splits the items to be sorted into two groups, recursively sorts each group, and merges them into a final, sorted sequence. Run time is Θ(n log n).

搜尋 (Search) 循序搜尋法 O(n) 二元搜尋法 O(log 2 n)