Wednesday, October 1, 2008

Dijkstra's algorithm for MatLab

යාන්ත‍ම් පො‍ඩි විවේකයක් ලැබුනා. මොනව හරි ලියන්න කියල හිත හිතා ඉන්නකොට මම ඊයෙ ලියපු Matlab code එකක් මතක් උනා.
පහත දැක්වෙන්නේ Dijkstra Algorithm එකයි. කාට හෝ මේක ප්‍රයෝජනවත් වේ යැයි මම හිතනවා. අවුරුදු ගානකට කලින් මෙම ඇල්ගොරිතම දැනන් හිටියට, මේක කොයි තරම් වෙනස් ආකාරයේ ගැටළු සදහා යොදාගත හැකිද යන්න මට වැටහුනේ මෑතකදී.

Following is an implementation of the Dijkstra's Algorithm for Matlab. This algorithm is a solution for the 'single source shortest path problem', which is useful in many applications.

This code can be pasted in a new .m file in Matlab. Then this can be used as a function.

Format of the Inputs:

src: and integer defining the source node.
ConMat: Connection Matrix, a square matrix. Each element (i,j) of the matrix defines the length of a direct link from node i to node j (if there exists a link). If there is no link from i to j, the element (i,j) must contain -1.

Output format:

The output is given in an array (and) of size N, where N is the number of nodes in the network. Each element of the output array corresponds to a Node with same index. Each element will contain the index of the node which is the closest to the corresponding node, in the shortest path to the source. The element corresponding to source node would contain -1.

Eg. If the source node is 2, and the shortest path from 2 to 7 is 2-->4-->7, then the output array would contain | x |-1| x | 2 | x | x | 4 |.
As seen, the 7th element directs us to 4 th node, which inturn directs us to node 2. Similary, the shortest path to each other node from the source node could be extracted from the output.


%An implementation of the Dijkstra's algorithm for MatLab
%(c)Buddhika Gunawardena

function ans = Dijkstra(src,ConMat)

N = size(ConMat,1);
Visited = -1*ones(N,1); % Here -1 means Not defined
PrevNode = -1*ones(N,1);
Distance = Inf*ones(N,1);

Distance(src) = 0;
CurrentNode = src;
nVisited = 0;
while (nVisited <N)
Visited(CurrentNode) = 1;
for i=1:N
if (ConMat(CurrentNode,i)>0)
temp = ConMat(CurrentNode,i) + Distance(CurrentNode);
if (temp< Distance(i))
Distance(i) = temp;
PrevNode(i) = CurrentNode;
end
end
end
minimum = 0;
for i= 1:N
if (Visited(i)<0)&&(Distance(i)>0)
if (minimum ==0)
minimum = i;
elseif (Distance(i)<Distance(minimum))
minimum = i;
end
end
end
CurrentNode = minimum;

nVisited = nVisited +1;
end

ans = PrevNode;



Thursday, September 25, 2008

මගේ පළමු සිංහල සටහන..

පුරා වසරක ඇවෑමෙන් මට මගේ වෙබ් ලොග් සටහන මතක් වුනේ සිංහල යුනිකොඩ් භාවිතය පුරුදු වීමට මිස වෙන හේතුවකට නොවේ. වැඩකටයුතු නම් ඉතා අධික වුවත් මෙයට උත්සාහයක් දැරිය යුතු බව මට සිතුනි. ඉංගිරීසියෙන් සියළු අධ්‍යාපන කටයුතු කරන අපට, සිංහල භාවිතයට ලැබෙන අවස්ථා බොහෝ විරලය. එම නිසි අක්ෂර වින්‍යාසයද බොහෝ විට වරදීම‍ට ඉඩ ඇත. එහෙත් ඉගෙනීමට නම් වැරදි කළ යුතු බව කවුරු හෝ පණ්ඩිතයෙක් කියා ඇති වග මට මතකය.
වෙනත් භාෂා වල වෙබ් පිටු සහ බ්ලොග දකින වාරයක් පාසා අපේ ‍මවුබසින් කොම්පියුටරය භාවිතා කිරීමට හැක්කේ කවදාදැයි සිතුනු අවස්තා අපමනයි. එනිසා මෙම යුනිකොඩ් ව්‍යායාමයට හවුල් වූ සියළු දෙනාටම තෙරුවන් සරණ පතමි.

Monday, July 16, 2007

Sri Lanka whitewash Bangaladesh..!!


I was lucky to witness the 3rd Test between Sri Lanka and Bangaladesh at Asgiriya grounds, on the fourth day, where bangaladeshi's were white washed clean by the Sri Lankan team.
When the match started on Wednesday, I was hoping atleast the rain would hold the match longer until the 4th day which is a Saturday, so that I could catch a glimpse of my favourite cricketers from my home town. The rain god came to my rescue, only abt half of the overs were played during the first day and even the third day I guess.
On saturday morning, I just watched a few overs from TV and thought the Bangaladeshis are gonna hold on to their innings for some time at least this time. Shahriar Nafees was goin well and he looked pretty ok in getting the bat to hit the ball, u know, unlike javed omar on the other end who seemed to be completedly bamboozeled by Lasith Malinga's Bowling. I went to town to attend to some other bunesses and in abt two hours looked at the score on my mobile and it was 6 wickets down all of a sudden. Oh man.. here they go again.. being slumped once again for the third time in 3 matches. I ran to the asgiriya ground in a few minutes, just in time to catch the fall of the last four wickets. Wow, it was so nice to see some live action after a long while. It was great since the whole thing was shown on Big Screen as well. That was a new experience for me.
I took some pics of the match using my k800i. On top is a panoramic view of the ground, while lasith malinga was on fire on the right side of the screen.
Murali passed his milestone of 700 test wickets in the match too. That is a real achievement and he is just 8 wickets behind shane warne.

Monday, July 2, 2007

Blogging..At last..

I guez it took a long time for me to decide whether I need to blog or not. Well, here we go..

Reading some of the blogs online, sometimes I feel whether it is really worth being called blogging.. I thought that 'Blog' means 'Web' + 'Log'. But who would keep a log of a whole lot of crap. So I guez, the more appropriate word would be somethin like 'Web'+'Crap', which obviously doesn't sound right. So, I guez I would simply call my blog, 'Buddhika's crap'

Hope somebody reads it once in a while.. hih hih hee..