0% found this document useful (0 votes)
48 views

Assignment 1

This document contains two assignments related to list processing and graph algorithms in MATLAB. The first assignment deals with common list processing operations like retrieving elements, slicing lists, and removing elements. It provides code examples to determine a specific element, slice the first few elements, print the last elements, and remove elements from different positions in a list. The second assignment implements depth-first search (DFS) graph traversal in MATLAB. It takes a graph represented by adjacency lists, a starting vertex, and optional parameters to traverse the whole graph or stop at a target vertex. The function returns arrays containing the discovery time, finish time, distance from the starting vertex, and predecessor for each vertex visited in the DFS order. It uses

Uploaded by

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

Assignment 1

This document contains two assignments related to list processing and graph algorithms in MATLAB. The first assignment deals with common list processing operations like retrieving elements, slicing lists, and removing elements. It provides code examples to determine a specific element, slice the first few elements, print the last elements, and remove elements from different positions in a list. The second assignment implements depth-first search (DFS) graph traversal in MATLAB. It takes a graph represented by adjacency lists, a starting vertex, and optional parameters to traverse the whole graph or stop at a target vertex. The function returns arrays containing the discovery time, finish time, distance from the starting vertex, and predecessor for each vertex visited in the DFS order. It uses

Uploaded by

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

ASSIGNMENT 1:

1. Determine nth element of list


(set 'phrase '("the" "quick" "brown" "fox" )
( nth 1 phrase );

Output: quick

2. Determine first n elements from list


(set 'alphabet '("a" "b" "c" "d" "e"))
(slice alphabet 0 3)
3. Determine last n elements from list
(set 'L '(1 2 3 4 5 6 7 8 9))
(define (print_last_n LL n)(dotimes (counter n)(print (LL (- counter)))))
(print_last_n L 6)
4. Remove first n elements from list
(set 'List '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16))
(define (remove_first_n LL n)(dotimes(counter n)(pop LL))LL)
(remove_first_n List 5)
5. Remove last n elements from list
(set 'List '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16))
(define (remove_last_n LL n) LL (dotimes(counter n)(pop LL -1))LL)
(remove_last_n List 5)
6. Remove nth element
(set 'List '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16))
(define (remove_nth LL n) LL (setq a 1) (dotimes(counter (-n 2)) (setq a (+a 1))) (pop LL a)) LL)
(remove_nth List 5)

ASSIGNMENT 2:
7. Dynamically modify the database of a program
(defun (my-func) (display "foo ") (display "bar ") (display "baz "))
(defun (main-loop)
(my-func)
(swap (first my-func) (second my-func))
(main-loop))
8. Implement DFS Algorithm in MATLAB
function [d dt ft pred] = dfs(A,u,full,target)
% DFS Compute depth first search distances, times, and tree for a graph
%
% [d dt ft pred] = dfs(A,u) returns the distance (d), the discover (dt) and
% finish time (ft) for each vertex in the graph in a depth first search
% starting from vertex u.
% d = dt(i) = ft(i) = -1 if vertex i is not reachable from u
% pred is the predecessor array. pred(i) = 0 if vertex (i)
% is in a component not reachable from u and i != u.
%
% [...] = dfs(A,u,1) continues the dfs for all components of the graph, not
% just the connected component with u
% [...] = dfs(A,u,[],v) stops the dfs when it hits the vertex v
%
% Note 1: When target is specified, the finish time array only records the
% finish time for all vertices that actually finished. The array will then
% be undefined on a significant portion of the graph, but that doesn't
% indicate the vertices are unreachable; they just haven't been reached
% yet.
if ~exist('full','var') || isempty(full), full=0; end
if ~exist('target','var') || isempty(full), target=0; end
if isstruct(A), rp=A.rp; ci=A.ci;
else [rp ci]=sparse_to_csr(A);
end
n=length(rp)-1;
d=-1*ones(n,1); dt=-1*ones(n,1); ft=-1*ones(n,1); pred=zeros(1,n);
rs=zeros(2*n,1); rss=0; % recursion stack holds two nums (v,ri)
% start dfs at u
t=0; targethit=0;
for i=1:n
if i==1, v=u;
else v=mod(u+i-1,n)+1; if d(v)>0, continue; end, end
d(v)=0; dt(v)=t; t=t+1; ri=rp(v);
rss=rss+1; rs(2*rss-1)=v; rs(2*rss)=ri; % add v to the stack
while rss>0
v=rs(2*rss-1); ri=rs(2*rss); rss=rss-1; % pop v from the stack
if v==target || targethit,
ri=rp(v+1); targethit=1; % end the algorithm if v is the target
end
while ri<rp(v+1)
w=ci(ri); ri=ri+1;

if d(w)<0
d(w)=d(v)+1; pred(w)=v;
rss=rss+1; rs(2*rss-1)=v; rs(2*rss)=ri; % add v to the stack
v=w; ri=rp(w);
dt(v)=t; t=t+1; continue; % discover a new vertex!
end
end
ft(v)=t; t=t+1; % finish with v
end
if ~full, break; end
end

You might also like