投影片 1

Similar documents
ENGG1410-F Tutorial 6

Microsoft PowerPoint - STU_EC_Ch08.ppt

Microsoft Word - template.doc

穨control.PDF

Untitled-3

<4D F736F F F696E74202D20B5DAD2BBD5C228B4F2D3A1B0E6292E BBCE6C8DDC4A3CABD5D>

2/80 2

高中英文科教師甄試心得

Open topic Bellman-Ford算法与负环

Microsoft PowerPoint - NCBA_Cattlemens_College_Darrh_B

Logitech Wireless Combo MK45 English

(Microsoft Word - \261M\256\327\272\353\302\262\263\370\247iEnd.doc)

Microsoft Word - TIP006SCH Uni-edit Writing Tip - Presentperfecttenseandpasttenseinyourintroduction readytopublish

untitled

Improved Preimage Attacks on AES-like Hash Functions: Applications to Whirlpool and Grøstl

1.ai

HPM 通 訊 第 八 卷 第 二 三 期 合 刊 第 二 版 數 學 歸 納 法 是 什 麼 玩 意 兒 中 原 大 學 師 資 培 育 中 心 楊 凱 琳 教 授 一 數 學 歸 納 法 不 同 於 歸 納 法 數 學 歸 納 法 在 數 學 知 識 的 領 域 中, 是 屬 於 基 本 原 理

<4D F736F F D205F FB942A5CEA668B443C5E9BB73A740B5D8A4E5B8C9A552B1D0A7F75FA6BFB1A4ACFC2E646F63>

PowerPoint Presentation

Windows XP


中国科学技术大学学位论文模板示例文档

untitled

穨1-林聖欽.doc

PowerPoint Presentation

2015年4月11日雅思阅读预测机经(新东方版)

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

Microsoft Word doc

國 史 館 館 刊 第 23 期 Chiang Ching-kuo s Educational Innovation in Southern Jiangxi and Its Effects ( ) Abstract Wen-yuan Chu * Chiang Ching-kuo wa

Microsoft Word - 1- 封面

Analysis of Cultural Elements of Meinong s Paper Umbrella Painting Abstract Meinong paper umbrellas are a traditional industrial art for the Hakka peo

摘 要 張 捷 明 是 台 灣 當 代 重 要 的 客 語 兒 童 文 學 作 家, 他 的 作 品 記 錄 著 客 家 人 的 思 想 文 化 與 觀 念, 也 曾 榮 獲 多 項 文 學 大 獎 的 肯 定, 對 台 灣 這 塊 土 地 上 的 客 家 人 有 著 深 厚 的 情 感 張 氏 於

第六章

Microsoft PowerPoint - CH 04 Techniques of Circuit Analysis

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

1505.indd

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

目 感恩与代祷 录 编 者 1 牧者心声 勒住你的舌头 龚明鹏 3 见证与分享 我的见证 吴权伟 8 相信就能够看见 卓艳梅 12 再述主恩 爱的雕凿 张英治 19 万怡杉 28 母亲节征文 记念母亲节 凌励立 43 父母的爱和神的爱 曹 红 47 Love Lisa Wang 50


Outline Speech Signals Processing Dual-Tone Multifrequency Signal Detection 云南大学滇池学院课程 : 数字信号处理 Applications of Digital Signal Processing 2

spss.doc

untitled

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

BC04 Module_antenna__ doc

1

[ 13 年 12 月 06 日, 下 午 6 点 24 分 ] Intel Hosts 新 加 入 的 同 学 们, 快 去 听 听 在 线 宣 讲 会 哦, 同 时 完 成 页 面 下 方 有 奖 调 查, 就 有 资 格 参 与 大 奖 抽 取 啦! [ 13 年 12 月 06 日, 下 午

untitled

中國的科學與中國的公民:大陸研究在台灣的困境\\

從詩歌的鑒賞談生命價值的建構

诚 实 守 信 公 平 交 易 好 的 伦 理 为 经 营 之 道 我 们 的 价 值 观 我 们 的 日 常 工 作 让 客 户 和 消 费 者 展 露 微 笑 我 们 关 注 员 工 产 品 和 业 务 的 不 断 改 善 和 进 步 我 们 珍 视 我 能 做 到 的 态 度 和 精 神, 尝

SuperMap 系列产品介绍

Microsoft Word - 論文封面 修.doc

Microsoft Word - Final Exam Review Packet.docx

国 培 简 讯 国 培 计 划 (2012) 示 范 性 集 中 培 训 项 目 国 培 计 划 (2012) 中 小 学 教 师 示 范 性 集 中 培 训 暨 中 西 部 农 村 教 师 集 中 培 训 中 小 学 骨 干 教 师 北 京 外 国 语 大 学 英 语 学 科 研 修 项 目 毕

<322DB57BA5C9BBF DA4BAA4E52E706466>

\\Lhh\07-02\黑白\内页黑白1-16.p

Preface This guide is intended to standardize the use of the WeChat brand and ensure the brand's integrity and consistency. The guide applies to all d

Microsoft Word - 十月號.doc

untitled


A Study on JI Xiaolan s ( ) Life, Couplets and Theories of Couplets 紀 曉 嵐 ( ) 生 平 資 料 斠 正 及 對 聯 聯 論 研 究 LI Ha 李 夏 THE UNIVER

Microsoft Word - A doc

(Microsoft Word - 11-\261i\256m\253i.doc)

89???????q?l?????T??

致 谢 开 始 这 篇 致 谢 的 时 候, 以 为 这 是 最 轻 松 最 愉 快 的 部 分, 而 此 时 心 头 却 充 满 了 沉 甸 甸 的 回 忆 和 感 恩, 一 时 间 竟 无 从 下 笔 虽 然 这 远 不 是 一 篇 完 美 的 论 文, 但 完 成 这 篇 论 文 要 感 谢

K301Q-D VRT中英文说明书141009

蔡 氏 族 譜 序 2

Edge-Triggered Rising Edge-Triggered ( Falling Edge-Triggered ( Unit 11 Latches and Flip-Flops 3 Timing for D Flip-Flop (Falling-Edge Trigger) Unit 11

國 立 政 治 大 學 教 育 學 系 2016 新 生 入 學 手 冊 目 錄 表 11 國 立 政 治 大 學 教 育 學 系 博 士 班 資 格 考 試 抵 免 申 請 表 論 文 題 目 申 報 暨 指 導 教 授 表 12 國 立 政 治 大 學 碩 博 士 班 論

研究論文 Assessment of Effectiveness of Passenger Seatbelt Reminder on Using Belt Rate - Toward Introducing Its Assessment in the New Car Assessm

k_club_ dvi

致 谢 本 人 自 2008 年 6 月 从 上 海 外 国 语 大 学 毕 业 之 后, 于 2010 年 3 月 再 次 进 入 上 外, 非 常 有 幸 成 为 汉 语 国 际 教 育 专 业 的 研 究 生 回 顾 三 年 以 来 的 学 习 和 生 活, 顿 时 感 觉 这 段 时 间 也

Chapter 1 Introduction

《應用倫理評論》第64期

区 域 活 动 进 入 中 班 我 们 区 域 的 设 置 和 活 动 材 料 都 有 所 变 化, 同 时 也 吸 引 孩 子 们 积 极 的 参 与 学 习 操 作 区 的 新 材 料 他 们 最 喜 欢, 孩 子 们 用 立 方 块 进 行 推 理 操 作 用 扑 克 牌 进 行 接 龙 游

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

硕 士 学 位 论 文 论 文 题 目 : 北 岛 诗 歌 创 作 的 双 重 困 境 专 业 名 称 : 中 国 现 当 代 文 学 研 究 方 向 : 中 国 新 诗 研 究 论 文 作 者 : 奚 荣 荣 指 导 老 师 : 姜 玉 琴 2014 年 12 月

國立桃園高中96學年度新生始業輔導新生手冊目錄

天 主 教 輔 仁 大 學 社 會 學 系 學 士 論 文 小 別 勝 新 婚? 久 別 要 離 婚? 影 響 遠 距 家 庭 婚 姻 感 情 因 素 之 探 討 Separate marital relations are getting better or getting worse? -Exp

從篤加有二「區」談當代平埔文化復振現相

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

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

18世纪东亚儒教思想的地形

國小三年級閱讀理解教學行動研究

論文格式

中山大學學位論文典藏


Microsoft PowerPoint - STU_EC_Ch04.ppt

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

99 學年度班群總介紹 第 370 期 班群總導 陳怡靜 G45 班群總導 陳怡靜(河馬) A 家 惠如 家浩 T 格 宜蓁 小 霖 怡 家 M 璇 均 蓁 雴 家 數學領域 珈玲 國燈 英領域 Kent

中國飲食色彩初探

第16卷 第2期 邯郸学院学报 年6月

南華大學數位論文


2-7.FIT)

:(104 :(104 )24:00 )~ :104 )~ )15:00~17:00 )08:00 : : :

encourages children to develop rich emotions through close contact with surrounding nature. It also cultivates a foundation for children s balanced de

Introduction to Hamilton-Jacobi Equations and Periodic Homogenization

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


Transcription:

Discrete Mathematics Chapter-10 Trees

Introduction to Tree ( 10.1) Def 1. A connected (undirected) graph that contains no simple circuits is called a tree. Trees are particularly useful in computer science, where they are employed in a wide range of algorithms. Construct efficient algorithms for locating items in a list. Construct efficient codes saving costs in data transmission and storage. Study games such as checkers and chess. Model procedure carried out using sequence of decisions.

The Bernoulli Family of Mathematicians Nikolaus (1623-1708) Jacob Ι (1654-1705) Nikolaus (1662-1716) Johann Ι (1667-1748 ) Nikolaus Ι (1687-1759) Nikolaus ΙΙ (1695-1726) Daniel (1700-1782) Johann ΙΙ (1710-1790) Johann ΙΙΙ (1746-1807) Jacob ΙΙ (1759-1789)

EXAMPLE2 Which of the graphs shown below are trees? a b d f c e 1 G 2 G b c d f 3 G 4 G b b c d d e f f a a a c e e Tree Tree Not a Tree Not a Tree

Theorem 1 An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. Pf: T is a tree. T is connected, no simple circuit. For any vertices x and y, there is a simple path. If there is a different path from x to y, then two different paths will form a circuit. This is a contradiction. Thus the path is from x to y is unique. Assume that there is a unique path between any two vertices in graph T. T is connected. Suppose that T contains a simple circuit. Then every pair of vertices in this circuit, say x and y, have two different paths from x to y. That contradicts to the uniqueness of the path. Thus T has no circuit, and then T is a tree.

Def 1. A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root. f g a c d a b e f b g c e d b a d e c A Tree A root tree with root a f g A root tree with root c

Terminology Ancestors Descendants parent Child Siblings Leaf Internal vertex Subtree

Terminology A Rooted Tree T b subtree of b c a f h i g a is the parent of f, b and g j is the parent of l and m f is the child of a e is the child of c f, b and g are siblings e and d are siblings j subtree of g d e a, b are ancestors of c e, c are descendants of b k l m leaves: d, e, f, k, i, l, m internal vertices: others

Def 3 A rooted tree is called an m-ary tree if every interval vertex has no more than m children. The tree is called a full m-ary tree if every interval vertex has exactly m children. An m-ary tree with m = 2 is called a binary tree.

Example 3 T T 1 2 A full binary tree. A full 3-ary tree.

T3 T4 A full 5-ary tree. A m-ary tree. (m 3) Not a full m-ary tree

More Terminology An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered (from left to right). In an ordered binary tree (usually called just a binary tree), if an internal vertex has two children, the first child is called the left child and the second one is called the right child. The tree rooted at the left (right) child of a vertex is called the left (right) subtree of this vertex.

Example 4 d b e a h c The Right Subtree of the Vertex c. The Left Subtree of the Vertex c. i f g j k l A Binary Tree T m

Trees as Models H H C H H H H H C H H C C C H H C H H H C H H H C H H H Isobutane Butane The Two Isomers of Butane

Representing Organizations President VP R&D VP Marketing VP Services VP Finance Director Resourch Director Software Development AVP Sales Chief Field Operations Director Accounting Director Software Development AVP Marketing Chief Field Operations Director MIS

Theorem 2 A tree with n verices has n 1 edges. Pf: Prove by induction. Let P(n) be the statement A tree with n verices has n 1 edges. Basis Step: When n = 1, a tree with one vertex has no edge. P(1) is true. Inductive Step: Assume that P(k) is true. Suppose that T is a tree with k+1 vertices and v is a leaf. Let w is the parent of v. Remove v and the edge connecting v and w. We ll get a tree T with k vertices and then has k 1edges. It follows that T has k edges. Then P(k+1) is true.

Theorem 3 A full m-ary tree with i internal vertices contains mi+1 vertices. T T 1 2 A full binary tree m = 2, i = 3, 7 vertices. A full 3-ary tree m = 3, i = 4, 13 vertices

T3 T4 A full 5-ary tree m = 5, i = 3, 16 vertices Not a full m-ary tree Not satisfy the theorem

Example 9 A chain letter: Each people sends to 4 people. Some people do this, but others do not send any letters. How many people have seen this letter, including the first person, if no one receives more than one letter and if the chain letter ends after there have 100 people who read it but didn t send it out? How many people sent out the letter? Sol: This is a 4-ary (m = 4) tree. Note there are n = mi +1 vertices and l = n i leaves. 100(= l) leaves n = 4(n 100) + 1 400 1 = 3n There are n = 133 vertices. There are 133 100 = 33 internal vertices.

Theorem 4 A full m-ary tree with 1. n vertices has i = (n 1)/m internal vertices and l = [(m 1)n + 1]/m leaves, 2. i interval vertices has n = mi + 1 vertices and l = (m 1)i + 1 leaves, 3. l leaves has n = (ml 1)/(m 1) vertices and i = (l 1)/(m 1) internal vertices.

More Terminology The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex v. The level of the root is defined to be zero. The height of a rooted tree is the maximum of the levels of vertices. That is, the height is the length of the longest path from the root to any vertex. A rooted m-ary tree of height h is balanced if all leaves are at levels h or h 1.

Example c b e f a j k l Level Vertices 0 a 1 b, j, k 2 c, e, f, l 3 d, g, i, m, n 4 h d g i m n h The height of this rooted tree is 4.

Example T T 1 2 Balanced Not balanced

T 3 Balanced

10.2 Applications of Trees

Introduction How should items in a list be stored so that an item can be easily located? What series of decisions should be made to find an object with a certain property in a collection of objects of a certain type? How should a set of characters be efficiently coded by bit strings?

Binary Search Tree 簡介 : 二元搜尋樹是一種二元樹 它可能是空的, 若不是空的, 它具有下列特性 : (1) 每一個元素有一鍵值, 而且每一元素的鍵值都不相同, 即每一個鍵值都是唯一的 (2) 在非空的左子樹上的鍵值, 必小於在該子樹的根節點中的鍵值 (3) 在非空的右子樹上的鍵值, 必大於在該子樹的根節點中的鍵值 (4) 左子樹和右子樹也都是二元搜尋樹

Example1 Form a binary search tree for the words mathematic physics geography zoology meteorology geology psychology chemistry (using alphabetical order)

Constructing a Binary Search Tree 按照題目所給的字順序排, 如題 Mathematics 就是 root 接下 Physic>Mathematics, 所以在 M 右下加一個 child Geography<Mathematics, 所以再 M 左下加一個 child Zoology>Mathematics 且 >Physics, 所以再 P 右下加一個 child Meteorology>Mathematics 但 <Physics, 所以在 P 左下加一個 child

Geology<Mathematics 但 >Geography, 所以在 Geography 的右下加一個 child Psychology>Mathematics 且 >Physics 但 <Zoology 所以在 Z 左下加一個 child Chemistry<Mathematics 且 <Geography, 所以在 Geography 的左下加一個 child

Huffman Codes 簡介 :Huffman 編制程式是試圖減少相當數量位元必需代表標誌串的一個統計技術 修造 Huffman 樹 (1) 選擇二個 parentless 結點以最低的可能性 (2) 創造是二個最低的可能性結點的父母的一個新結點 (3) 分配新結點可能性相等與它的孩子的可能性的總和 (4) 重覆步驟 1 直到有只一個 parentless 結點剩下

Example 5 Use Huffman coding to encode the fallowing symbols with the frequencies listed: A:0.08, B:0.10, C:0.12, D:0.15, E:0.20, F:0.35. What is the average number of bits used to encode a character?

Example 5 於題目中找尋最小兩點 A:0.08 和 B:0.10(A 於右下 B 於左下 B>A) 接著把 A+B 視為新的一點 0.18, 再找最小兩點 C:0.12 和 D:0.15 如同 AB 方法接起來 C+D 也視為新的一點 0.27 再比大小, 之後依此方法作完全部的點 而全圖於右下角的支線都為 1, 左下角都之線都為 0 因此 A by 111,B by 110,C by 011,D by 010, E by 10, F by 00. So the average number of bits used to encode a symbol using this encoding is 3 0.08+3 0.10+3 0.12+3 0.15+2 0.20+2 0.35=2.45

9.3 Tree Traversal

Universal address system We will describe one way to order totally the vertices of an ordered rooted tree. To produce this ordering, we must first label all the vertices. We do this recursively : 1. Label the root with the integer 0. Then label its k children (at label 1) from left to right with 1,2,...,k. 2. For each vertex v at level n with label A, label its k v children, as they are drawn from left to right, with A.1,A.2, A.k v.

Example 1 we display the labelings of the universal address system next to the vertices in the ordered rooted tree shown in Figure 1. Sol. The lexicographic ordering of the labelings is 0 < 1 < 1.1 < 1.2 < 1.3 < 2 < 3 < 3.1 < 3.1.1 < 3.1.2 < 3.1.2.1 < 3.1.2.2 < 3.1.2.3< 3.1.2.4 < 3.1.3 < 3.2 <4 < 4.1 < 5 < 5.1 < 5.1.1 < 5.2 < 5.3

0 1 2 3 4 5 1.1 1.2 1.3 3.1 3.2 4.1 5.1 5.2 5.3 3.1.1 3.1.2 3.1.3 5.1.1 3.1.2.1 3.1.2.2 3.1.2.3 3.1.2.4

Def. 1 Let T be an order rooted tree with root r. If T consists only of r, then r is the preorder traversal of T. Otherwise, suppose that T 1, T 2,, T n are the subtree at r from left to right in T. The preorder traversal begins by visiting r. It continues by traversing T 1 in preorder, then T 2 in preorder, and so on, until T n is traversed in preorder.

Example 2 In which order does a preorder traversal visit the vertices in the ordered rooted tree T shown below. a j e k b f c l g h m d i n o p

a b e f c d g h i j k n o p l m a b e j k n o p f c d g l m h i The preorder traversal of T is a, b, e, j, k, n, o, p, f, c, d, g, l, m, h, i.

Def. 2 Let T be an ordered rooted tree with root r. If T consists Only of r, then r is the inorder traversal of T. Otherwise, suppose that T 1, T 2,, T n are the subtree at r from left to right in T. The inorder traversal begins by traversing T 1 in inorder, then visiting r. It continues by traversing T 2 in inorder, then T 3 in inorder,, and Finally T n in inorder.

Example 3 In which order does a inorder traversal visit the vertices in the ordered rooted tree T shown below. a j e k b f c l g h m d i n o p

b a c d e f g h i j k l m n o p j e k b f a c l g m d h i n o p The preorder traversal of T is a, b, e, j, k, n, o, p, f, c, d, g, l, m, h, i.

Def. 3 Let T be an ordered rooted tree with root r. If T Consists only of r, then r is the postrder traversal of T. Otherwise, suppose that T 1, T 2,, T n are the subtreesat r from left to right in T. The postorder traversal begins by traversing T 1 in postorder, then T 2 in inorder, then T 3 in inorder,, and ends by visiting r.

Example 4 In which order does a postorder traversal visit the vertices in the ordered rooted tree T shown below. a j e k b f c l g h m d i n o p

b c d a e f g h i j k l m n o p j e k b f a c l g m d h i n o p The preorder traversal of T is a, b, e, j, k, n, o, p, f, c, d, g, l, m, h, i.

Infix, Prefix, and Postfix notation Example 5 what is the ordered rooted tree that represents the expression((x+y) 2)+((x-4)/3)?

+ _ + x y x 4 / / + 2 _ 3 + 2 _ 3 x y x 4 x y x 4

Example 6 what is the prefix from for ((x+y) 2)+((x-4)/3)? Example 8 what is the postfix from for ((x+y) 2)+((x-4)/3)? + / + 2 _ 3 x y x 4 This produces the prefix expression : + + x y 2 / - x 4 3This produces the postefix expression : x y + 2 x 4-3 / +

Example 7 what is the value of the prefix expression + - * 2 3 5 / 2 3 4? + - * 2 3 5 / 2 3 4 + - * 2 3 5 / 8 4 + - * 2 3 5 2 + - 6 5 2 + 1 2 3

Example 9 what is the value of the postfix expression 7 2 3 * - 4 9 3 / +? 7 2 3 * - 4 9 3 / + 7 6-4 9 3 / + 1 4 9 3 / + 1 9 3 / + 1 3 + 4

Example 10 Find the ordered rooted tree representing the compound proposition ( (p ^ q)) ( p ˇ q). Then use this rooted tree to find the prefix, postfix and infix firms of this expression. ^ p q p q ˇ ˇ ^ ^ p q p q p q p q preorder : ^ p q ˇ p q postorder : p q ^ p q ˇ inorder : (( (p ^ q)) (( p) ˇ ( q)))

10.4 Spanning Trees ( 9.4) Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G. Spanning Tree of a graph is not unique.

Theorem A simple graph is connected if and only if it has a spanning tree. Pf: Suppose that a simple graph G has a spanning tree T. T contains every vertex of G. There is a path in T between any two of its vertices. Thus there is a path in G between any two of its vertices. Hence, G is connected. Suppose that G is connected. If G is not a tree, it must contain a simple circuit. Removing an edge from this circuit obtains a subgraph of G, say G. If G is a tree, than it is a spanning tree of G. Otherwise, we continue to remove edge from a circuit until we get a tree subgraph. This tree is a spanning tree of G.

Depth-First Search a b c d i j e f h k g Arbitrary choose a vertex as a root. Successively adding vertices and edges where each new edge is incident with the last vertex in the path and a vertex not already in the path. Let this path as long as possible. Move back to the next to last vertex in the path. a c d e b f i g h k j

Algorithm: Depth-First Search procedure DFS(G; connected graph with vertices v 1, v 2,, v n ) T := tree consisting only of the vertex v i visit(v i ) procedure visit(v: vertex of G) for each vertex w adjacent to v and not yet in T begin add vertex w and edge {v, w} to T visit(w) end The procedure DFS constructs a spanning tree using O(e), or O(n 2 ). DFS is also called backtracking, since the algorithm returns to vertices previously visited to add paths.

Example How can backtracking be used to decide whether a given graph can be colored using 3 colors? e d a 1 a b c b2 a b c d e c1 c3 Spanning Tree d 3 d 1 e no way e 3 Done

Breadth-First Search d h m a b c e i Arbitrary choose a root. k f j l g Add all edges (and vertices) incident to this vertex. Order these vertices (in level 1) arbitrarily. Continue this step for the vertices we obtaining in previous step as long as it does not produce a simple circuit till all vertices have been added. c b a d h e i f m k j l g

Algorithm: Breadth-First Search procedure BFS(G; connected graph with vertices v 1, v 2,, v n ) T := tree consisting only of the vertex v i L := empty list put v 1 in the list L of unprocessed vertices while L is not empty begin remove the first vertex, v, from L for each neighbor w of v if w is not in L and not in T then begin add w to the end of the list L add w and edge {v, w} to T end end The procedure DFS constructs a spanning tree using O(e), or O(n 2 ).

DFS in Directed Graphs a b c d e f g h The output of DFS of a directed graph is not a tree, but a spanning forest. i j k l g c a b f e d h l k i j

10.5 Minimum Spanning Trees A minimum spanning tree in a (weighted) connected graph is a spanning tree that has the smallest possible sum of weights of its edges. The first algorithm was given by Robert Prim in 1957. Choose any edge with smallest weight. Successively add to the tree edges of minimum weight that are incident to the vertex already in the tree and not forming a simple circuit. Stop when n 1 edges have been added.

Algorithm Prim s Algorithm procedure Prim(G: weighted connected undirected graph with n vertices) T := a minimum-weight edge for i := 1 to n 2 begin e := an edge of minimum weight incident to a vertex in T and not forming a simple circuit in T if added to T T := T with e added end (T is a minimum spanning tree of G)

Example San Francisco $900 $2000 $1200 Denver $2200 $1300 700 (Chicago, Atlanta) 800 (Atlanta, New York) 1200 (San Francisco, Chicago) 900 (Denver, San Francisco) Chicago $1000 $1600 $1400 $700 $800 New York Chicago Atlanta Atlanta S F New York Denver Total cost = $3600

Example e a b c d 3 4 2 3 3 f 1 i j k l g 3 3 4 2 4 3 3 2 1 1 5 h 1 (c, d) 2 (c, g) 3 (b, c) 1 (b, f) 2 (a, b) 2 (f, j) 3 (a, e) 3 (i, j) 3 (g, h) 3 (h, l) 1 (k, l) Total = 24

Algorithm Kruskal s Algorithm procedure Kruskal(G: weighted connected undirected graph with n vertices) T := empty tree for i := 1 to n 1 begin e := an edge in G with smallest weight that does not form a simple circuit when added to T T := T with e added end (T is a minimum spanning tree of G)

Example San Francisco $900 $2000 $1200 Denver $2200 $1300 Chicago $1600 $1400 $1000 $700 $800 Atlanta 700 (Chicago, Atlanta) 800 (Atlanta, New York) 900 (Denver, San Francisco) 1200 (San Francisco, Chicago) Total cost = $3600 New York

Example e a b c d 3 4 2 3 3 f 1 i j k l g 3 3 4 2 4 3 3 2 1 1 5 h Total = 24 1 (c, d) 1 (b, f) 1 (k, l) 2 (a, b) 2 (c, g) 2 (f, j) 3 (b, c) 3 (g, h) 3 (a, e) 3 (h, l) 3 (i, j)