Use bfs function included here.
https://github.com/prabushitha/DataStructures/blob/master/Graph/GraphBFS.py
Output:
bfs(graph,source) returns an array with shortest distances for each vertex in the graph
array element is None when there's no path
Usage:
If you want to print distances from 3 to other vertexes, call bfs function with the source vertex 3.
print(bfs(graph,3))
Saturday, November 14, 2015
Tuesday, November 10, 2015
Creating a graph according the user input
Firstly,
මේ පයිතන් graph class 2ක දාගන්න (class Node and class Graph)
https://raw.githubusercontent.com/prabushitha/DataStructures/master/Graph/GraphAL.py
Okay!
මේ පයිතන් graph class 2ක දාගන්න (class Node and class Graph)
https://raw.githubusercontent.com/prabushitha/DataStructures/master/Graph/GraphAL.py
Okay!
දැන් User input දෙන්නේ මේ විදියට කියලා හිතමු
1 2
1 3
2 4
4 5
END
ඉහත ආකාරය දක්වා ඇත්තේ
Edge එක පටන් ගන්න Vertex එක <SPACE> Edge එක ඉවර වෙන Vertex එක
eg. 1 2 කියලා කියන්නේ
පලවෙනි Vertex එකේ ඉදන් දෙවනි Vertex එකට Edge එකක් තියන්වයි කියල.
END කියලා input එක දුන්නම එතනින් ඉවරයි කියලා ගන්නවා
So, the graph should look like this

ඕක graph එකට දාගන්න කෝඩ් එක දැන් අපි ලියමු.
graph = Graph()
while(True):
userInput = str(input())
if(userInput=="END"):
break
vertexFrom = userInput.split()[0]
vertexTo = userInput.split()[1]
#Creating the vertexes if vertexes are not already created
if(not graph.isVertex(vertexFrom)):
graph.addVertex(vertexFrom)
if(not graph.isVertex(vertexTo)):
graph.addVertex(vertexTo)
graph.addEdge(vertexFrom,vertexTo)
print(graph)
ඔච්චර තමා කරන්න තියෙන්නේ. ඊට පස්සේ තියෙන්නේ හදාගත්ත graph එකෙන් වැඩ ගන්න එක :-D
ඒ කියන්නේ BFS දාල Shortest path හොයන සීන් වගේ එව්වා :-D
<download fullcode here: https://raw.githubusercontent.com/prabushitha/DataStructures/master/Graph/Example/GraphExample.py >
1 3
2 4
4 5
END
ඉහත ආකාරය දක්වා ඇත්තේ
Edge එක පටන් ගන්න Vertex එක <SPACE> Edge එක ඉවර වෙන Vertex එක
eg. 1 2 කියලා කියන්නේ
පලවෙනි Vertex එකේ ඉදන් දෙවනි Vertex එකට Edge එකක් තියන්වයි කියල.
END කියලා input එක දුන්නම එතනින් ඉවරයි කියලා ගන්නවා
So, the graph should look like this

ඕක graph එකට දාගන්න කෝඩ් එක දැන් අපි ලියමු.
graph = Graph()
while(True):
userInput = str(input())
if(userInput=="END"):
break
vertexFrom = userInput.split()[0]
vertexTo = userInput.split()[1]
#Creating the vertexes if vertexes are not already created
if(not graph.isVertex(vertexFrom)):
graph.addVertex(vertexFrom)
if(not graph.isVertex(vertexTo)):
graph.addVertex(vertexTo)
graph.addEdge(vertexFrom,vertexTo)
print(graph)
ඔච්චර තමා කරන්න තියෙන්නේ. ඊට පස්සේ තියෙන්නේ හදාගත්ත graph එකෙන් වැඩ ගන්න එක :-D
ඒ කියන්නේ BFS දාල Shortest path හොයන සීන් වගේ එව්වා :-D
<download fullcode here: https://raw.githubusercontent.com/prabushitha/DataStructures/master/Graph/Example/GraphExample.py >
Monday, November 9, 2015
Graph implementation using linkedlists
Here's my python class(Includes class Node and class Graph)
https://raw.githubusercontent.com/prabushitha/DataStructures/master/Graph/GraphAL.py
Usage
#After copying/importing above 2 classes to your code
#Creating a new graph
graph = Graph()
#Adding a vertex. e.g add 2 vertexes A and B
graph.addVertex("A")
graph.addVertex("B")
#Adding an edge between 2 vertexes
graph.addEdge("A","B")
#Check whether there's an edge between 2 vertexes. e.g from A to B (returns True/False)
graph.isEdge("A","C")
#Check whether there's a path from one vertex to another. e.g from A to B (returns True/False)
graph.isPath("D","B")
Monday, December 9, 2013
Memory Management via Garbage Collection [Actionscript 3]
In actionscript you can force garbage collection (GC) to run, but it's a bad idea if you do so.
But we can't predict at what time it'll run even if you force it to run.
GC runs once a while and you can make objects eligible to dispose by using several methods.
System.gc();But we can't predict at what time it'll run even if you force it to run.
GC runs once a while and you can make objects eligible to dispose by using several methods.
- Unregister all listeners
- Stop all timers and intervals
- Stop playheads like sounds,videos.
- Deactivate objects that would become unreachable if the object itself became unreachable. (nullify)
Tuesday, November 19, 2013
Lesson 01 - Installing Java
Before starting with anything, we have to set up a java background to work with it in our PC. All you need to do is,
>>>Command prompt to setting up the path<<<
% java -version
This will print the version of the java executable, if it can find it. If you get error java: Command not found. Then path is not properly set.
To find out which java executable the first one found in your PATH, execute:
% which java
Below are the steps to set the PATH permanently,
Note: We are here giving instructions for two most popular Shells on Linux and Solaris.
Please visit link below if you are using any other shells.
Path Setting Tutorial
For bash Shell:
For C Shell (csh):
- Download java via http://www.java.com/en/download/
- Install
- Setting up the path
>>>Command prompt to setting up the path<<<
- First go to java installed folder --> jdkx.x.x_xx -->bin and copy the path
- Then open command prompt ([Window key+R] then type cmd)
- Go to the folder you expected to do your java work using cmd.
(*See the note below) - Then type, set path=%path%;your bin path copied
eg. set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin
- to change the drive, type the drive name: eg. D:
- to change the folder type cd folderName eg. cd Umesh
- to clear the path and go back to the drive type cd/
OR
>>>>>>Advaced Settings<<<<<<
Windows 8
- Drag the Mouse pointer to the Right bottom corner of the screen
- Click on the Search icon and type: Control Panel
- Click on -> Control Panel -> System -> Advanced
- Click on Environment Variables, under System Variables, find PATH, and click on it.
- In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
- Close the window.
- Reopen Command prompt window, and run your java code.
Windows 7
- Select Computer from the Start menu
- Choose System Properties from the context menu
- Click Advanced system settings > Advanced tab
- Click on Environment Variables, under System Variables, find PATH, and click on it.
- In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
- Reopen Command prompt window, and run your java code.
Windows XP
- Start -> Control Panel -> System -> Advanced
- Click on Environment Variables, under System Variables, find PATH, and click on it.
- In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
- Close the window.
- Reopen Command prompt window, and run your java code.
Windows Vista
- Right click My Computer icon
- Choose Properties from the context menu
- Click Advanced tab (Advanced system settings link in Vista)
- In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
- Reopen Command prompt window, and run your java code.
Setting Path on Solaris and Linux
To find out if the java executable is in your PATH, execute:% java -version
This will print the version of the java executable, if it can find it. If you get error java: Command not found. Then path is not properly set.
To find out which java executable the first one found in your PATH, execute:
% which java
Below are the steps to set the PATH permanently,
Note: We are here giving instructions for two most popular Shells on Linux and Solaris.
Please visit link below if you are using any other shells.
Path Setting Tutorial
For bash Shell:
- Edit the startup file (~/ .bashrc)
- Modify PATH variable:
PATH="$PATH":/usr/local/jdk1.6.0/bin - export PATH
- Save and close the file
- Open new Terminal window
- Verify the PATH is set properly
% java -version
For C Shell (csh):
- Edit startup file (~/ .cshrc)
- Set Path
set path="$PATH":/usr/local/jdk1.6.0/bin - Save and Close the file
- Open new Terminal window
- Verify the PATH is set properly
% java -version
Monday, November 18, 2013
Floating bubbles [Actionscript 3]
- Open Flash new document.
- Create a small ball shape movieclip in the library.
- Give it a AS linkage name "Ball".
- Creating the ball. [Random place, Random Size, Random Color ...].
- Displaying the ball on stage.
- Moving the ball.
//stage size
= 926 x534 px
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.geom.ColorTransform;
var Bx;
var By;
var Bd;
var Ba;
var red;
var green;
var blue;
/*
Assigning variables
ball x position = Bx
ball y position = By
ball diameter = Bd
ball opacity = Ba
red = red color amount of the
ball
green = green color amount of the ball
blue = color amount of the ball
*/
function createBall():void{
Bx = Math.floor(Math.random()*926)+1;
//Max value =
926 Min value = 1
By = -Math.floor(Math.random()*534)-54;
//according to the
values, ball creates above the stage
Bd = Math.floor(Math.random()*20)+10;
Ba = Math.floor(Math.random()*99)+60;
var ball:Ball = new Ball();
ball.x = Bx;
ball.y = By;
ball.width = Bd;
ball.height = Bd;
red = Math.random();
green = Math.random();
blue = Math.random();
var myColour:ColorTransform = new
ColorTransform(red+0.6,green,blue-0.2);
ball.transform.colorTransform = myColour;
ball.alpha = Ba/100;
var xposInf = Math.floor(Math.random()*5)-2;
var yposInf = Math.floor(Math.random()*5)-2;
function moveBall(e:Event):void{
ball.x+=xposInf;
ball.y+=yposInf;
}
// Adding the ball on stage
stage.addChild(ball);
//Moving the ball according to frame rate.
stage.addEventListener(Event.ENTER_FRAME,moveBall);
/*
To save the memory usage
after 60 seconds ball created will be removed
*/
var timer:Timer = new Timer(60000,1);
timer.addEventListener(TimerEvent.TIMER,removeBall);
function removeBall(e:Event):void{
stage.removeChild(ball);
}
timer.start();
}
/*
A function to create a ball
Ball x,y,size,opacity values generates
randomly as well as color value.
xposInf is a variable for horizontal(x)
speed of the ball (random value)
yposInf is a variable for vertical (y)
speed of the ball (random value)
*/
// A function to create 2 balls at a time
function addBalls(e:Event):void{
createBall();
createBall();
}
// creating two balls on stage continuously
stage.addEventListener(Event.ENTER_FRAME,addBalls);
//adding a significant amount(100) balls at the start
for(var i=0;i<100;i++){
createBall();
}
Wednesday, November 13, 2013
x,y Camera using scrollRect [Actionscript 3]
In this tutorial, we are going to make a 2D camera (moves along x and y axises).
- First make your backround (Better if it's larger than stage) and give it an instance name "Background"
- According to the press of arrow keys camera gonna move.
Let's get started...
<code>
import
flash.geom.Rectangle;
import
flash.events.Event;
import
flash.events.KeyboardEvent;
var
myCam:Rectangle = new
Rectangle(0,0,stage.stageWidth,stage.stageHeight);
/* creating a rectangle
which act as a camera to a movieClip which is our Background
parameters for new rectangle
parameters for new rectangle
Rectangle x position = 0
Rectangle y position = 0
Rectangle width = stage width
Rectangle height = stage height
Rectangle y position = 0
Rectangle width = stage width
Rectangle height = stage height
*/
//creating a function in
order to move the camera (i.e. our rectangle) using arrow keys
function
moveCamera(e:KeyboardEvent):void{
if(e.keyCode==Keyboard.UP){
myCam.y-=2;
}
if(e.keyCode==Keyboard.DOWN){
myCam.y+=2;
}
if(e.keyCode==Keyboard.LEFT){
myCam.x-=2;
}
if(e.keyCode==Keyboard.RIGHT){
myCam.x+=2;
}
}
//calling the camera move
function to the stage
stage.addEventListener(KeyboardEvent.KEY_DOWN,moveCamera);
function
updateView(e:Event):void{
Background.scrollRect = myCam;
}
/*
assigning viewing area of the Background to the
rectangle(our cam)
so, we can see the area bounded to the rectangle more like a mask
but you can't use .mask instead of scrollRect :D
note that myCam position, scale parameters generates in, as myCam is
inside Background
so, we can see the area bounded to the rectangle more like a mask
but you can't use .mask instead of scrollRect :D
note that myCam position, scale parameters generates in, as myCam is
inside Background
*/
stage.addEventListener(Event.ENTER_FRAME,
updateView);
/* calling the updateView function on stage. stage gets updated
according to the frame
rate and the function updateView
rate and the function updateView
*/
</code>
- Note : scrollRect animations are not well smooth.
- Light Blue colour texts represent variables and functions
- Green texts represent comments
Subscribe to:
Posts (Atom)
