Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Advanced Algorithms HW 8 (Martin Valgur)

1654 views
1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
% Programming/Coding Assignment
3
% LaTeX Template
4
%
5
% This template has been downloaded from:
6
% http://www.latextemplates.com
7
%
8
% Original author:
9
% Ted Pavlic (http://www.tedpavlic.com)
10
%
11
% Note:
12
% The \lipsum[#] commands throughout this template generate dummy text
13
% to fill the template out. These commands should all be removed when
14
% writing assignment content.
15
%
16
% This template uses a Perl script as an example snippet of code, most other
17
% languages are also usable. Configure them in the "CODE INCLUSION
18
% CONFIGURATION" section.
19
%
20
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21
22
%----------------------------------------------------------------------------------------
23
% PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
24
%----------------------------------------------------------------------------------------
25
26
\documentclass{article}
27
28
%\usepackage{lmodern}
29
\usepackage{fourier} % Use the Adobe Utopia font for the document - comment this line to return to the LaTeX default
30
\usepackage{fancyhdr} % Required for custom headers
31
\usepackage{lastpage} % Required to determine the last page for the footer
32
\usepackage{extramarks} % Required for headers and footers
33
\usepackage[usenames,dvipsnames]{color} % Required for custom colors
34
\usepackage{graphicx} % Required to insert images
35
\usepackage{listings} % Required for insertion of code
36
\usepackage{courier} % Required for the courier font
37
\usepackage[english]{babel} % English language/hyphenation
38
\usepackage{amsmath,amsfonts,amsthm} % Math packages
39
\usepackage{pdfpages}
40
\usepackage{color}
41
\usepackage{float}
42
\usepackage{tabularx}
43
\PassOptionsToPackage{hyphens}{url}\usepackage{hyperref}
44
\usepackage{subfig}
45
\usepackage{scalefnt}
46
\restylefloat{table}
47
48
% Margins
49
\topmargin=-0.45in
50
\evensidemargin=0in
51
\oddsidemargin=0in
52
\textwidth=6.5in
53
\textheight=9.0in
54
\headsep=0.25in
55
56
\linespread{1.1} % Line spacing
57
58
% Set up the header and footer
59
\pagestyle{fancy}
60
\lhead{\hmwkAuthorName} % Top left header
61
\chead{\hmwkClass: \hmwkTitle} % Top center head
62
\rhead{\firstxmark} % Top right header
63
\lfoot{\lastxmark} % Bottom left footer
64
\cfoot{} % Bottom center footer
65
\rfoot{Page\ \thepage\ of\ \protect\pageref{LastPage}} % Bottom right footer
66
\renewcommand\headrulewidth{0.4pt} % Size of the header rule
67
\renewcommand\footrulewidth{0.4pt} % Size of the footer rule
68
69
\setlength\parindent{0pt} % Removes all indentation from paragraphs
70
71
%----------------------------------------------------------------------------------------
72
% CODE INCLUSION CONFIGURATION
73
%----------------------------------------------------------------------------------------
74
75
\definecolor{MyDarkGreen}{rgb}{0.0,0.4,0.0} % This is the color used for comments
76
\lstloadlanguages{Perl} % Load Perl syntax for listings, for a list of other languages supported see: ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/listings/listings.pdf
77
\lstset{language=Python, % Use Perl in this example
78
frame=single, % Single frame around code
79
basicstyle=\small\ttfamily, % Use small true type font
80
keywordstyle=[1]\color{Blue}\bf, % Perl functions bold and blue
81
keywordstyle=[2]\color{Purple}, % Perl function arguments purple
82
keywordstyle=[3]\color{Blue}\underbar, % Custom functions underlined and blue
83
identifierstyle=, % Nothing special about identifiers
84
commentstyle=\usefont{T1}{pcr}{m}{sl}\color{MyDarkGreen}\small, % Comments small dark green courier font
85
stringstyle=\color{Purple}, % Strings are purple
86
showstringspaces=false, % Don't put marks in string spaces
87
tabsize=5, % 5 spaces per tab
88
%
89
% Put standard Perl functions not included in the default language here
90
morekeywords={rand},
91
%
92
% Put Perl function parameters here
93
morekeywords=[2]{on, off, interp},
94
%
95
% Put user defined functions here
96
morekeywords=[3]{test},
97
%
98
morecomment=[l][\color{Blue}]{...}, % Line continuation (...) like blue comment
99
numbers=left, % Line numbers on left
100
firstnumber=1, % Line numbers start with line 1
101
numberstyle=\tiny\color{Blue}, % Line numbers are blue and small
102
stepnumber=5 % Line numbers go in steps of 5
103
}
104
105
% Creates a new command to include a perl script, the first parameter is the filename of the script (without .pl), the second parameter is the caption
106
\newcommand{\pythonscript}[2]{
107
\begin{itemize}
108
\item[]\lstinputlisting[caption=#2,label=#1]{#1.py}
109
\end{itemize}
110
}
111
112
%----------------------------------------------------------------------------------------
113
% DOCUMENT STRUCTURE COMMANDS
114
% Skip this unless you know what you're doing
115
%----------------------------------------------------------------------------------------
116
117
% Header and footer for when a page split occurs within a problem environment
118
\newcommand{\enterProblemHeader}[1]{
119
\nobreak\extramarks{#1}{#1 continued on next page\ldots}\nobreak
120
\nobreak\extramarks{#1 (continued)}{#1 continued on next page\ldots}\nobreak
121
}
122
123
% Header and footer for when a page split occurs between problem environments
124
\newcommand{\exitProblemHeader}[1]{
125
\nobreak\extramarks{#1 (continued)}{#1 continued on next page\ldots}\nobreak
126
\nobreak\extramarks{#1}{}\nobreak
127
}
128
129
\setcounter{secnumdepth}{0} % Removes default section numbers
130
\newcounter{homeworkProblemCounter} % Creates a counter to keep track of the number of problems
131
132
\newcommand{\homeworkProblemName}{}
133
\newenvironment{homeworkProblem}[1][Problem \arabic{homeworkProblemCounter}]{ % Makes a new environment called homeworkProblem which takes 1 argument (custom name) but the default is "Problem #"
134
\stepcounter{homeworkProblemCounter} % Increase counter for number of problems
135
\renewcommand{\homeworkProblemName}{#1} % Assign \homeworkProblemName the name of the problem
136
\section{\homeworkProblemName} % Make a section in the document with the custom problem count
137
\enterProblemHeader{\homeworkProblemName} % Header and footer within the environment
138
}{
139
\exitProblemHeader{\homeworkProblemName} % Header and footer after the environment
140
}
141
142
\newcommand{\problemAnswer}[1]{ % Defines the problem answer command with the content as the only argument
143
\noindent\framebox[\columnwidth][c]{\begin{minipage}{0.98\columnwidth}#1\end{minipage}} % Makes the box around the problem answer and puts the content inside
144
}
145
146
\newcommand{\homeworkSectionName}{}
147
\newenvironment{homeworkSection}[1]{ % New environment for sections within homework problems, takes 1 argument - the name of the section
148
\renewcommand{\homeworkSectionName}{#1} % Assign \homeworkSectionName to the name of the section from the environment argument
149
\subsection{\homeworkSectionName} % Make a subsection with the custom name of the subsection
150
\enterProblemHeader{\homeworkProblemName\ [\homeworkSectionName]} % Header and footer within the environment
151
}{
152
\enterProblemHeader{\homeworkProblemName} % Header and footer after the environment
153
}
154
155
%----------------------------------------------------------------------------------------
156
% NAME AND CLASS SECTION
157
%----------------------------------------------------------------------------------------
158
159
\newcommand{\hmwkTitle}{Homework\ 8} % Assignment title
160
\newcommand{\hmwkDueDate}{Thursday,\ October 30,\ 2014} % Due date
161
\newcommand{\hmwkClass}{Advanced Algorithmics} % Course/class
162
\newcommand{\hmwkClassTime}{} % Class/lecture time
163
\newcommand{\hmwkClassInstructor}{} % Teacher/lecturer
164
\newcommand{\hmwkAuthorName}{Martin Valgur} % Your name
165
166
%----------------------------------------------------------------------------------------
167
% TITLE PAGE
168
%----------------------------------------------------------------------------------------
169
170
\title{
171
\vspace{2in}
172
\textmd{\textbf{\hmwkClass:\ \hmwkTitle}}\\
173
\normalsize\vspace{0.1in}\small{Due\ on\ \hmwkDueDate}\\
174
\vspace{0.1in}\large{\textit{\hmwkClassInstructor\ \hmwkClassTime}}
175
\vspace{3in}
176
}
177
178
\author{\textbf{\hmwkAuthorName}}
179
\date{\today} % Insert date here if you want it to appear below your name
180
181
%----------------------------------------------------------------------------------------
182
183
\begin{document}
184
185
%\maketitle=
186
%\thispagestyle{empty}
187
188
%----------------------------------------------------------------------------------------
189
% TABLE OF CONTENTS
190
%----------------------------------------------------------------------------------------
191
192
%\setcounter{tocdepth}{1} % Uncomment this line if you don't want subsections listed in the ToC
193
194
\newpage
195
%\tableofcontents
196
%\newpage
197
198
%----------------------------------------------------------------------------------------
199
% PROBLEM 1
200
%----------------------------------------------------------------------------------------
201
202
% To have just one problem per page, simply put a \clearpage after each problem
203
204
\begin{homeworkProblem}
205
\textit{Use the same graph as from last week. Make the SCC component graph, and provide it's topological sort order.}\\
206
207
Sage worksheet with the source code of this solution: \url{https://cloud.sagemath.com/projects/b60781fb-41d7-40bf-97f4-94455eaaf2d4/files/HW8/Task\%201.sagews}.
208
209
\begin{figure}[H]
210
\label{fig:scc}
211
\centering
212
\includegraphics[width=1\textwidth]{T1_SCCs.pdf}
213
\caption{The graph with its vertices colored by the SCC they belong to.}
214
\end{figure}
215
\begin{figure}[H]
216
\label{fig:scc_topo}
217
\centering
218
\includegraphics[width=0.6\textwidth]{T1_toposort.pdf}
219
\caption{Topological ordering of the SCCs.}
220
\end{figure}
221
222
223
\end{homeworkProblem}
224
225
\clearpage
226
227
\begin{homeworkProblem}
228
\textit{Take the same graph as from task 1 (last week). Represent it in Adjacency Matrix presentation. Calculate the versions with exactly 2 hops and 3 hops. $G*G$ and $G*G*G$ respectively. Draw the "3-hop graph". Calculate the transitive closure by 1) $G*G*G*\cdots*G$ ; 2) Power steps: $G_2=G*G$ ; $G_4=G_2*G_2$ ; $G_8=G_4*G_4$ ; $G_{16}=G_8*G_8$, and 3) Warshall algorithm.}\\
229
230
Sage worksheet containing the source code of this solution: \url{https://cloud.sagemath.com/projects/b60781fb-41d7-40bf-97f4-94455eaaf2d4/files/HW8/Task\%202.sagews}.
231
232
%\noindent\null\hfill\includegraphics[width=0.4\textwidth]{T2_G.pdf} \hfill
233
%$$\input{T2_G.tex}$$ \hfill\null
234
235
\begin{figure}[H]
236
\hfill
237
\vcenter{\subfloat{
238
$\input{T2_G.tex}$
239
}}
240
\hspace*{-3in}
241
\vcenter{\subfloat{\includegraphics[width=0.4\textwidth]{T2_G.pdf} }}
242
\hfill
243
\caption{Adjacency matrix $G$.}
244
\end{figure}
245
246
\begin{figure}[H]
247
\hfill
248
\vcenter{\subfloat{
249
$\input{T2_G2.tex}$
250
}}
251
\hspace*{-3in}
252
\vcenter{\subfloat{\includegraphics[width=0.4\textwidth]{T2_G2.pdf} }}
253
\hfill
254
\caption{Adjacency matrix $G*G$.}
255
\end{figure}
256
257
\begin{figure}[H]
258
\hfill
259
\vcenter{\subfloat{
260
$\input{T2_G3.tex}$
261
}}
262
\hspace*{-3in}
263
\vcenter{\subfloat{\includegraphics[width=0.4\textwidth]{T2_G3.pdf} }}
264
\hfill
265
\caption{Adjacency matrix $G*G*G$.}
266
\end{figure}
267
268
\begin{figure}[H]
269
\hfill
270
\vcenter{\subfloat{
271
$\input{T2_tc.tex}$
272
}}
273
\hspace*{-3in}
274
\vcenter{\subfloat{\includegraphics[width=0.4\textwidth]{T2_tc.pdf} }}
275
\hfill
276
\caption{Transitive closure (calculated with either $((G+I)^N-I)$ or the Warshall algorithm).}
277
\end{figure}
278
279
\end{homeworkProblem}
280
281
\begin{homeworkProblem}
282
\textit{Take the same graph and run the simple random walk procedure along the graph. Treat all outbound links with equal probability. Report the proportion of time spent in each node. Add some edges to get rid of dead ends (to node C, for example K-C, I-C, G-C). And some edges to get into nodes that had no incoming links (D-J, D-L). Interpret the effect of those added links.}\\
283
284
See the Sage worksheet for this task: \url{https://cloud.sagemath.com/projects/b60781fb-41d7-40bf-97f4-94455eaaf2d4/files/HW8/Task\%203.sagews}.\\
285
286
287
The addition of edges to dead-end nodes got rid of all absorbing states. As a result the random walk can continue indefinitely and the probability of finishing the walk on a specific node does not depend on the starting node anymore.
288
289
\end{homeworkProblem}
290
291
\begin{homeworkProblem}
292
\textit{Explore the real graphs from here: \url{https://courses.cs.ut.ee/2013/algorithmics/spring/Main/HW8}. Select two graphs by your own choice. Describe the graphs - what they are about? Analyse the degree distribution; find how many components.}\\
293
294
See the Sage worksheet for this task: \url{https://cloud.sagemath.com/projects/b60781fb-41d7-40bf-97f4-94455eaaf2d4/files/HW8/Task\%204.sagews}.\\
295
296
\begin{itemize}
297
\item Enron -- directed graph of emails sent from person to person in the Enron company.
298
\item Students -- undirected graph of collaboration between students during a course, edges have different types to mark different types of collaboration.
299
\item US airlines -- directed graph of the number of flights between US airports. Surprisingly has many SCCs. The degree distribution obeys inverse power law.
300
\item Karate -- a small undirected graph of relationships between people who belong to either or both of two clubs.
301
\end{itemize}
302
303
\end{homeworkProblem}
304
305
\begin{homeworkProblem}
306
\textit{What is the diameter of your graphs (components) from 2? (you can estimate it also by running shortest distance not from all nodes, if computationally too slow. How slow?).}\\
307
308
See the Sage worksheet for this task (same as in task 4): \url{https://cloud.sagemath.com/projects/b60781fb-41d7-40bf-97f4-94455eaaf2d4/files/HW8/Task\%204.sagews}.\\
309
310
\end{homeworkProblem}
311
312
\begin{homeworkProblem}
313
\textit{Continue task nr 3. Represent it as a adjacency matrix with real-valued probabilities. Compare whether the matrix multiplication yields similar stationary distributions. Both on original and modified graph (with no dead ends or unreachable nodes). }\\
314
315
See the Sage worksheet for task 3: \url{https://cloud.sagemath.com/projects/b60781fb-41d7-40bf-97f4-94455eaaf2d4/files/HW8/Task\%203.sagews}.\\
316
317
\end{homeworkProblem}
318
319
320
%----------------------------------------------------------------------------------------
321
322
\end{document}
323