0% found this document useful (0 votes)
25 views19 pages

Final Notes Chapter1 7 1

This chapter discusses graph search algorithms and matrix multiplication. It introduces depth-first search (DFS) to explore graphs with vertices and edges. DFS has a runtime of O(V+E) where V is the number of vertices and E is the number of edges. It also introduces topological sorting to find a linear ordering of vertices in a directed acyclic graph (DAG). The runtime of computing a topological order is O(V+E). Additionally, it discusses Strassen's matrix multiplication algorithm which improves on the naive O(n3) runtime for multiplying two n×n matrices. Strassen's algorithm runs in O(nlog27) ≈ O(n2.

Uploaded by

Myummie e
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views19 pages

Final Notes Chapter1 7 1

This chapter discusses graph search algorithms and matrix multiplication. It introduces depth-first search (DFS) to explore graphs with vertices and edges. DFS has a runtime of O(V+E) where V is the number of vertices and E is the number of edges. It also introduces topological sorting to find a linear ordering of vertices in a directed acyclic graph (DAG). The runtime of computing a topological order is O(V+E). Additionally, it discusses Strassen's matrix multiplication algorithm which improves on the naive O(n3) runtime for multiplying two n×n matrices. Strassen's algorithm runs in O(nlog27) ≈ O(n2.

Uploaded by

Myummie e
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 1

0 Notation
-

Let f and be functions If 7


g a
.

constant cent and a number K s -


t

flnlfcgcn ) b- nzk then we write

flnl =
Olgin) ) OR f =
0cg)

d- Notation
Let f and be functions If 7 constant
g
a
.

cent and a number KEN St flnlzegln)


n7k then we write
:
H
f- Mlg)
fln) =D lgln) ) OR

G- Notation
Let fond be functions if 7 two constants
g
.

s.tqglnkflnkczg.tn/-VnZk
G. GEIR and KEN
then we write :

fln / = ① ( gcn) ) OR F- ② (g)


Chapter 2

Sort Strassen Algo


Merge
Problem Sort
Problem Matrix Multiplication
:
:
array Input : Matrix M , and Mz of
Input :
Array of size n size nxn

Output : Sorted array output M , ✗ Mz:

Runtime :O / n
login) Runtime Olnlostd :
)
S, Biz Bzz SEA ,tA,z
-
=
,

2 I 4 3
53 Az, 1- Azz 54=132,1-13
-

2 1 9 13 •

2
I 4 3

11 2 314
Master theorem
.

1121314
'
Tlnl={a.TL%)
n -4
+ nd n >1

a > I b> I 020


Selection
Problem Find
Algosmallest d>
logbla) TIM -_
olndl
: kth
D= log , /a)
Input Arrays :
of size Tlnkolndlogln))
dclogbla)
n
and int k TlnI=O(n' 09 blay
, oaken
Output -
: K th -

smallest
elem ins
Runtime: Oln)

Finding Runtime :

Unfolding Induction :
Let be
Tlnl -11%1
n
large and assume
n Tlmlsc.vn for
+
all
k-mcn-tl.IT/+Z&n n

:

Chapter 3

Order
Depth-first Search / DFS) Topo
undirected
Problem : Explore directed
Problem : Explore graph
graphs
with vettices
Input Directed
: graph G
Graph C-
Input : a
w/ no
cycles
order of C-
✓ and
edges E Output Topo ;

Output None : A > C


n
F

Runtime: 0114+11=-1) r
n do B > D VE
A
d
89C
2
B) >
B→ A S
→,c→E

3 14s 6 1 2
,
3
D E 4 5 g

DFS -

Forest compute Topo


116
1 : Run DFS
"
A
12%1 '
A 1215 Legend 2 : Run bucket sort

? I C
¢ : tree edges on Postnumbers
/ q
✗ v10 \ e
'
: forward edges 3 : Reverse
posterder
,zyy T : back edges
4¥ Runtime :O / IVHIEI)
' F-
g. Cross edges

l '

iii. ←

! -
'

sg6⇐ compute if graph


is cyclic
C- has directed A Run Dfslwlprefpostl
cycle
:
a
if
⇐> 2: _V non -
tree edge
Prelclcprecvkpostcvk Postal
DFS forest has
-
a
baekedge
Runtime : OWNED
(

Chapter 3

Dijkstra's Algo
Problem Find : the shortest path between 2 vertices
in weighted directed
a
,
graph
1- input Directed
:

graph G w/ vertices V
and
edges Fe
,
where each
edge ahas a weight .

A source Vector SEV

Output :

the shortest path between s and vector VEV


any
Runtime ONE / +
XD logllvll)
:

Ys
at >✗
^
t ✗ y z

r ^ 1 0 10 ✗ 5 X
lo
2 3 a 8 14 7
6 2
S y
3 8 13
r 5
9 / T
Yy 2
Yz
4 I

7
Chapter 4

Greedy Algo :

An that arrives
algorithm at a solution by '

making of choices , each of which looked


a
sequence
best at the time

Making Change
Problem How can
: a
given amount of
money
be made
w/ the least number of fins with gives demons
Afn put : An of denominations and
integer v
array ,
an

Output the least number


: of coins needed to make

the value V
1. 65 -

1
Coins :{0.05, 0.1 0.25 1,2 }
, ,
0.65 -

0.25
Sum : 1.65
0.4 -

0 .

25
0.15 -

0.10
0.05 -

0.05

knapsack
Fon put objects weight end
n
each object
:
st has a
a valve and a maximum weight W

Output A vector ✗ {✗i. }


.

=
Xz,✗, .
. .
,xn St
-
Of ✗if I -
the total value ✗
, Ytxzvzt txn is
maximised
.
. -

the total
weight
-


txzwzt EW
w
+
, , . . -

Xniwn
Runtime Olnlogln ) ) :
Chapter 4

knapsack lemma
knapsack example
If the objects are
I 1 2 3 4 5
✗g- I selected in
Vi 20 30 66 40 Go decreasing
✗i lo 20 30 40 50
4=1 Viwi , then the greedy
✗2=1
choice finds an optimal
'

2 1.5 2.2 I 1.2


×
; Xg= 0.8
solution
W= 100 Sum __
164

Minimum LMST)
Spanning Tree
Given undirected connected
a
weighted , , Graph G- Cv,r
,

MSTG of
'
an G is sub
a
graph such that :
-

← The vertex set of G' is V


'
-

G is connected
Sum of the
weights of
-

edges in minimum
'
G is

8 7
$ • •

9
4 2
11
y

• 14 •
,
10
8
••• -

• •
I z

Lemma : For a Graph G- ( v. E) , split V into A and B .

shortest
edge connected
be A and B
Let { 44 G- E the .

Then there is MST of G


containing {v.v}
an
Chapter 4

Kruskal
5
MST of G 6
Problem: Find an A c E

Approach : Maintain a forest 4


5 3 H
step , add an edge
z
on each p
of min weight that doesn't B D F
create a
cycle z y

Runtime :
Olmlogln)) BGBD.CD.CF.tt#DFEF,.A/B,CE,Ac
1 2 3 4

Prim
start :
-
A-
T
is a set consisting
of
of an
arbitrary vertex
- is an
empty set
edges
Q is all vertices in G- not in A
-

Freq , let :

of any edge and


miniweightlv
vertex in A
) : min
weight v a

closest 14 : vertex u in A- s.twtlv.ir)= minweightlv)


Iteration :

Select the vertice ✗ st min minimum


-

weight 1×1 is
-
Add ✗ to A and remove it from Q
-
add
{× ,
closest 1×1 } to T
-
update closest and minmeight for all vertices
not in A. connected to ✗

Runtime : Olmloglnl )
Chapter 4

Prim -

example
6 5 b
A B C D
1
>
1 23 5 5 7
2 46

F- 2
43
5
F C- H
,

:
Chapter 5 General structure
B. Structure the optimal
solution
2. Write a recursion for
the optimal solution
3. Solve the recurrence

Matrix Multiplication
Problem : Find the optimal order to multiply a set
of matrices
together matrices Al Az Az
F- input :
A set of n
, ,
An where
, , . . .

A, is Po XP,
,
Az is P, Pz ,

,
Antis
. .
Pn i ✗ Pn
. .
-
-

Output : the number of steps needed to multiply all


matrices optimally

,j)={iI¥jk
0 if i=j
mli
mliikltmlktl 's ) -1 Pi
, Pkpj ) lficjsn ,

Runtime : 01ns)
-

Longest common
subsequence (Lcs)
Problem : Find the LCS between two sets

Input : 2
sequences
✗ and Y
between ✗ and Y
Output : the LCS

Runtime : 01m .nl

{[a×(
i = 0 or j = 0

Cli , j ) = 1 + Cli -

1, j -

1) i >o ,j >o and ✗


i=Yj
efi,j -11 di I ,j )) i > -0
,j > 0
-

and ✗ i =/
,
Yj
Chapter 5

Floyd -
War shall

Problem : Find the shortest path between all pairs of


vertices in a
weighted directed,
graph C-

ftp.nput : Graph G- IV. E) ,


G- is directed and
edges are weighted
For Asian ,: dist ( ii. 01--0

For A fish If Jen


, , izj :

,j,o)={
tliii ) if
distli edge fisj ) exists

else
For Asian , 1£ js.in Is Ken :
,

distci,j,k)=min{
distli.j.k-H.distli.kik-iltdistlkis.tl-11)
Runtime: 01h31
Chapter 6

A decision problem is a
problem for which

the answer is Yes or no

class p Class NP ( non-deterministic)


NP if is in
A problem A :

set of all problems For given input Ii


-

that can be solved the answer to ACI) is


yes
then 1- a proof es .t
in time
:

polynomial -
C is polynomial to the
length of I
-
In Polynomial time ,
Problems in P we
verify that
can

C is a correct proof
graph bipartite
?
-
Is a

Is connected ? that ACII Yes


graph
-

-
-
a

1-1am Cycle-
Problem
Input Undirected graph
:
G- =
IV. E)

Question : Does G- contain a hamiltonian cycle?


• • Unknown
if in P



Chapter 6

Traveling Salesman Problem LTSP) Problem


Input :
A. complete directed graph
-

G- LYE)
,
where
each
edge is
weighed
An
- K
integer
Question : Does G contain a hamiltonian cycle
of total at most
weight K?

Unknown if in p

Subset Sum - Problem


Input :

A of
5={5,756/23,2902}
set
integers g
-

f- 2963
-
an
integer K
Yes ! { 5,5612902}
'
S
Question : Is
-

there a subset g)
of S Unknown if in P
s.tt#s.x--t ?
Chapter 6
Definition of P
A
The
language L is in P if 7 an
algo
and a
constant cat sit for any input ✗

-
✗ EL Alex)= Yes
-
The runtime of Atx ) is olnc) where n

is the
length of ✗

Definition of Np ( Non -
deterministic Polynomial)
The language L is in NP if F- an
algor
and a Constante 21 s -
t for any input ✗

✗ EL ⇐ 1- a certificate yet
-
Iyl =
011×14
-
Vlx, y ) =
Yes
- VCX /
y)
runtime is polynomial
of
to the
length ✗

Theorem Language
NP The
language
of

P c- NP a Problem
decision
p is the set of all
inputs for which
the answer is

yes or no
Chapter 6
Reductions Famous 3 Properties
'
L and L
For 2 languages
J
.

1. f maps inputs for Ltoli


L is polynomial-time
we
say
reducible to L
'
if -7 function
2. It inputs ✗ for L
a '

✗ c- L⇐> flxl
C- L

f which satisfies the


3. inputs ✗ for L ,
famous three properties '
flx) is 0cm for
Notation : L

CZI
Lsp

CLIQUE Problem
K=4
Input :
• • •

graph

,

- An undirected C-
An integer K •
••
-

Question : Does G contain •

a clique of size K

INDEP -
SET
k =3
Problem
In plot : ⑨ •

\
- An undirected graph C-
- An integer K

Question Does : C- contain
an independent set of size
K


INDEP -
SET
Ep CLIQUE
Chapter 6 Theorem
If LEPL '
and L' EP
then LEP

VERTEX COVER - Problem


k=4
Input :
undirected ⑨
graph G

An

-

-
An int k •

Question :
Does ' G contain ⑧ ⑨ ⑨
a vertex cover of size K
-

CLIQUE ftp.VERTEX -

COVER

3. SAT
µ%npUt ?
10=1> ✗ vyv> 2) nlxv > yvz )A(xvyvz't
y
"

-
A boolean formula of the
Y ✗

form 10=9^9 ^ . . -
Mcm
'

Clause
↳ ci-ljvli.ve , literal
7✗ 72 TY -2

Question : Is at satisfiable? ×

- Does there exist a truth


value for the literals Y z

s.to/--l3SATfp.INDEP- SET
Chapter 6
NP-Hard NP-Complete
A L is NP-complete
A
language
L is NP-Hard
if
language
:
if Y L' ENP, L'
Ep L -
LENP
t L'c- NP L' fpl
Not covered
-

Theorem Million $
Then
Assume L is NP-complete Question
LEP < P=NP

Theorem

}
L NP-complete
is
'
L NP-complete
'
is
Lfp L
L' GNP

'
show L is NP-complete
1. Show L' ENP
2. Look for "
similar
"
problem L that
is NP Complete
-

'
3. Show Lfpl
Chapter 6
CIRCUIT -
SAT
Input :

Directed acyclic graph AND/OR Gates have


- -
-

whose vertices in
degree
are -

2
boolean
gates NOT Gates
-

have
Known input gates
-

in
have in
degree 0 and
-

degree I
are TRUE or FALSE
-
There is one output
-
Unknown input gates gate
is 0
,
whose out
degree
have
and
in
are
degree
labelled ?
0

Question : Is possible to truth value


it
assign
-

to each Inputs .t is TRUE


unknown the output

complete
'

CIRCUIT SAT is NP -
-

CIRCUIT
-
SAT fp SAT 3
Chapter 7
Theorem
In the decision tree moder
,

of numbers
sorting array n
an

takes rent
ogcnll time

Theorem
In the decision tree model ,

in sorted
finding the position of × a

array A -11 .n] takes D. llogcnl) time


. .

Theorem
Icn) time to find the
max
It takes
element in an A- [ 1 n]
array
. - •

Theorem
It takes h1n1 steps to compute
the median of an unsorted array AII . . ]
n

You might also like