没有合适的资源?快使用搜索试试~ 我知道了~
首页Introduction to Algorithms Lecture Notes (MIT 6.006)
Introduction to Algorithms Lecture Notes (MIT 6.006)
5星 · 超过95%的资源 需积分: 13 42 下载量 177 浏览量
更新于2023-03-16
评论 1
收藏 18.19MB PDF 举报
Introduction to Algorithms Lecture Notes (MIT 6.006)
资源详情
资源评论
资源推荐
Lecture 1 Introduction and Document Distance 6.006 Spring 2008
Lecture 1: Introduction and the Document
Distance Problem
Course Overview
• Efficient procedures for solving problems on large inputs (Ex: entire works of Shake-
speare, human genome, U.S. Highway map)
• Scalability
• Classic data
structures and elementary algorithms (CLRS text)
Real implementations in Python Fun problem sets! • ⇔
β version
of the class - feedback is welcome! •
Pre-requisites
• Familiarity with Python and Discrete Mathematics
Contents
The course is divided into 7 modules - each of which has a motivating problem and problem
set (except for the last module). Modules and motivating problems are as described below:
1. Linked
Data Structures: Document Distance (DD)
2. Hashing: DD,
Genome Comparison
3. Sorting: Gas
Simulation
4. Search:
Rubik’s Cube 2 × 2 × 2
5. Shortest Paths:
Caltech MIT→
6. Dynamic Programming:
Stock Market
7. Numerics:
√
2
Document Distance Problem
Motivation
Given
two documents, how similar are they?
• Identical
- easy?
• Modified
or related (Ex: DNA, Plagiarism, Authorship)
1
Lecture 1 Introduction and Document Distance 6.006 Spring 2008
• Did Francis
Bacon write Shakespeare’s plays?
To
answer the above, we need to define practical metrics. Metrics are defined in terms of
word frequencies.
Definitions
1. Word:
Sequence of alphanumeric characters. For example, the phrase “6.006 is fun”
has 4 words.
2. Word
Frequencies: Word frequency D(w) of a given word w is the number of times
it occurs in a document D.
For
example, the words and word frequencies for the above phrase are as below:
Count
: 1 0 1 1 0 1
W ord : 6 the is 006 easy fun
In practice,
while counting, it is easy to choose some canonical ordering of words.
3. Distance
Metric: The document distance metric is the inner product of the vectors D
1
and D
2
containing the word frequencies for all words in the 2 documents. Equivalently,
this is the projection of vectors D
1
onto D
2
or vice versa. Mathematically this is
expressed as:
D
1
· D
2
= D
1
(w) · D
2
(w) (1)
w
4. Angle Metric: The angle between the vectors D
1
and D
2
gives an indication of overlap
between the 2 documents. Mathematically this angle is expressed as:
θ(
D
1
, D
2
) = arccos
D
1
· D
2
� D
1
� ∗ � D
2
�
0 ≤ θ ≤ π/
2
An angle
metric of 0 means the two documents are identical whereas an angle metric
of π/2 implies that there are no common words.
5. Number
of Words in Document: The magnitude of the vector D which contains word
frequencies of all words in the document. Mathematically this is expressed as:
N(
D) =� D �=
√
D D (2)·
So let’s
apply the ideas to a few Python programs and try to flesh out more.
2
Lecture 1 Introduction and Document Distance 6.006 Spring 2008
Document Distance in Practice
Computing Document Distance: docdist1.py
The python
code and results relevant to this section are available here. This program com-
putes the
distance between 2 documents by performing the following steps:
Read file
•
• Make
word list [“the”,“year”,. . . ]
• Count
frequencies [[“the”,4012],[“year”,55],. . . ]
• Sort into
order [[“a”,3120],[“after”,17],. . . ]
• Compute θ
Ideally,
we would like to run this program to compute document distances between writings
of the following authors:
Jules Verne
- document size 25k •
• Bobsey Twins
- document size 268k
Lewis and
Clark - document size 1M •
• Shakespeare
- document size 5.5M
Churchill
- document size 10M •
Experiment:
Comparing the Bobsey and Lewis documents with docdist1.py gives θ = 0.574.
However, it takes approximately 3 minutes to compute this document distance, and probably
gets slower as the inputs get large.
What is wrong with the efficiency of this program?
Is it a Python vs. C issue? Is it a choice of algorithm issue - θ(n
2
) versus θ(n)?
Profiling: docdist2.py
In order
to figure out why our initial program is so slow, we now “instrument” the program
so that Python will tell us where the running time is going. This can be done simply using
the profile module in Python. The profile module indicates how much time is spent in each
routine.
(See this
link for details on profile).
The profile module is imported into docdist1.py and the end of the docdist1.py file is
modified. The modified docdist1.py file is renamed as docdist2.py
Detailed results
of document comparisons are available here
3
.
Lecture 1 Introduction and Document Distance 6.006 Spring 2008
More on
the different columns in the output displayed on that webpage:
• tottime per
call(column3) is tottime(column2)/ncalls(column1)
• cumtime(column4)includes
subroutine calls
• cumtime
per call(column5) is cumtime(column4)/ncalls(column1)
The profiling
of the Bobsey vs. Lewis document comparison is as follows:
Total: 195 secs •
Get words
from line list: 107 secs •
• Count-frequency:
44 secs
• Get words
from string: 13 secs
Insertion sort: 12 secs •
So the
get words from line list operation is the culprit. The code for this particular section
is:
word_list =
[ ]
for line
in L:
words_in_line = get_words_from_string(line)
word_list = word_list + words_in_line
return word_list
The bulk
of the computation time is to implement
word_list =
word_list + words_in_line
There isn’t
anything else that takes up much computation time.
List Concatenation:
docdist3.py
The problem
in docdist1.py as illustrated by docdist2.py is that concatenating two lists
takes time proportional to the sum of the lengths of the two lists, since each list is copied
into the output list!
L = L
1
+ L
2
takes time proportional to L
1
| + | L
2
. If we had n lines (each with one | |
n(n+1)
2
)word), computation time would be proportional to 1 + 2 + 3 + . . . + n =
2
= θ(n
Solution:
word_list.extend (words_in_line)
[word_list.append(word)] for each word in words_in_line
Ensures L
1
.extend(L
2
) time proportional to | L
2
|
4
剩余149页未读,继续阅读
绝不原创的飞龙
- 粉丝: 1w+
- 资源: 1091
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论3