Saturday, November 30, 2013
How To Install IIS (Internet Information Server) on Windows
Today I am going to show how to install IIS on your computer.
Go To Control Panel => Programs. In Programs and Features section click 'Turn Windows Features On or Off' . (Or simply type 'appwiz.cpl' and Run) .Then follow the images....
Then Tick Internet Information Server Section
Then open your browser and type 'localhost' in address bar and enter to check whether IIS is working in your computer
Thursday, February 28, 2013
Find Greatest Common Devicer Using Java
import java.util.*;
public class GCD{
public static void main(String args[]){
int integer1,integer2,GCD;
Scanner scanner = new Scanner(System.in);
try{
System.out.print("\n\n\nEnter Two Integers(separate by space) : ");
integer1 = scanner.nextInt();
integer2 = scanner.nextInt();
if(integer1==0||integer2==0){
System.out.println("Error:Can not find GCD for 0");
System.exit(-1);
}
GCD=GCD(integer1,integer2);
System.out.println("GCD of "+integer1+" and "+integer2+" is "+GCD);
}catch(Exception e){
System.out.println("Error:Integer values must be given");
}
}
private static int GCD(int integer1,int integer2){
int temp=0;
if(integer1>integer2){
while(integer1>0){
temp=integer2 % integer1;
integer2=integer1;
integer1=temp;
}
return integer2;
}
else if(integer1<integer2){
while(integer2>0){
temp=integer1 % integer2;
integer1=integer2;
integer2=temp;
}
return integer1;
}
else{
return integer1;
}
}
}
Find Prime Factors Of Given Integer Using Java
import java.util.*;
public class primeFactor{
public static void main(String args[]){
int integer;
Scanner scanner = new Scanner(System.in);
List<Integer> factors = new ArrayList<Integer>();
try{
System.out.print("\n\n\nEnter Integer : ");
integer = scanner.nextInt();
if(integer==0){
System.out.println("Error:Can not find primes for 0");
System.exit(-1);
}
System.out.print(integer+" = ");
for (Integer integer1 : FindSmallestFactors(integer)) {
System.out.print(integer1+" ");
}
}catch(Exception e){
System.out.println("Error:Integer values must be given");
}
}
private static List<Integer> FindSmallestFactors(int integer){
List<Integer> factors = new ArrayList<Integer>();
for (int i = 2; i <= integer; i++) {
while (integer % i == 0) {
factors.add(i);
integer = integer / i;
}
}
return factors;
}
}
Find Prime Numbers Using Java
import java.util.*;
public class Primes{
public static void main(String args[]){
int integer;
Scanner scanner = new Scanner(System.in);
List<Integer> factors = new ArrayList<Integer>();
try{
System.out.print("\n\n\nEnter Integer : ");
integer = scanner.nextInt();
if(integer==0){
System.out.println("Error:Can not find primes for 0");
System.exit(-1);
}
for (Integer integer1 : Primes(integer)) {
System.out.print(integer1+"\t");
}
}catch(Exception e){
System.out.println("Error:Integer values must be given");
}
}
private static List<Integer> Primes(int integer){
List<Integer> factors = new ArrayList<Integer>();
for (int i = 1; i <= integer; i++) {
for(int j=2;j<=i;j++){
if(i==j){
factors.add(i);
break;
}
//value is not prime
if(i %j==0){
break;
}
}
}
return factors;
}
}
Using Sieve of Eratosthenes
About Sieve of Eratosthenes. This method is more faster than above prime number finding method. import java.util.*;
//create a structure
class list{
int value;
char flag;
}
public class ex{
public static void main(String args[]){
int integer;
Scanner scanner = new Scanner(System.in);
try{
System.out.print("\n\n\nEnter Integer : ");
//get integer
integer = scanner.nextInt();
if(integer==0){
System.out.println("Error:Can not find primes for 0");
System.exit(-1);
}
PrimesSieveOfEratosthenes(integer);
}catch(Exception e){
System.out.println("Error:Integer values must be given");
}
}
private static void PrimesSieveOfEratosthenes(int integer){
list integers[]=new list[integer];
//initialize list elements
for(int m=0;m<integers.length;m++){
integers[m]=new list();
}
//add values to list elements
for(int i=1;i<integer;i++){
integers[i].value=i+1;
integers[i].flag='0';
}
//find prime numbers
for(int k=2;k<=integer;k++){
if(integers[k-1].flag=='0'){
for(int j=2*k-1;j<integer;j=j+k){
integers[j].flag='1';
}
}
}
//print prime numbers
for(int l=1;l<integer;l++){
if(integers[l].flag=='0'){
System.out.print(integers[l].value+"\t");
}
}
}
}
Wednesday, February 27, 2013
File Encryption and Decryption using AES Symmetric key Cryptographic algorithm
In this work, i encrypt a web page by running EncryptFile.java, then host that encrypted file to the server. Then another client can download that file from the server and decrypt it by running DecryptFile.java if that client has secret key.
You can download my work out HERE
EncrypFile.java
import java.security.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class EncryptFile {
public static void main(String args[]) {
if (args.length < 1) {
System.out.println("Usage: java EncryptFile <file name>");
System.exit(-1);
}
try {
File aesFile = new File("Encrypted Web Page.html");
FileInputStream fis;
FileOutputStream fos;
CipherInputStream cis;
//Creation of Secret key
String key = "MySEcRetKeY";
int length=key.length();
if(length>16 && length!=16){
key=key.substring(0, 15);
}
if(length<16 && length!=16){
for(int i=0;i<16-length;i++){
key=key+"0";
}
}
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");
//Creation of Cipher objects
Cipher encrypt =Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
encrypt.init(Cipher.ENCRYPT_MODE, secretKey);
// Open the Plaintext file
try {
fis = new FileInputStream(args[0]);
cis = new CipherInputStream(fis,encrypt);
// Write to the Encrypted file
fos = new FileOutputStream(aesFile);
byte[] b = new byte[8];
int i = cis.read(b);
while (i != -1) {
fos.write(b, 0, i);
i = cis.read(b);
}
fos.flush();
fos.close();
cis.close();
fis.close();
} catch(IOException err) {
System.out.println("Cannot open file!");
System.exit(-1);
}
} catch(Exception e){
e.printStackTrace();
}
}
}
DecryptFile.java
import java.security.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.net.*;
public class DecryptFile {
public static void main(String args[]) {
try {
File aesFile = new File("Downloaded Encrypted Web Page.html");
// if file doesnt exists, then create it
if (!aesFile.exists()) {
aesFile.createNewFile();
}
aesFile=retrieve(args);
File aesFileBis = new File("Decrypted Web Page.html");
FileInputStream fis;
FileOutputStream fos;
CipherInputStream cis;
//Creation of Secret key
String key = "MySEcRetKeY";
int length=key.length();
if(length>16 && length!=16){
key=key.substring(0, 15);
}
if(length<16 && length!=16){
for(int i=0;i<16-length;i++){
key=key+"0";
}
}
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");
//Creation of Cipher objects
Cipher decrypt =Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
decrypt.init(Cipher.DECRYPT_MODE, secretKey);
// Open the Encrypted file
fis = new FileInputStream(aesFile);
cis = new CipherInputStream(fis, decrypt);
// Write to the Decrypted file
fos = new FileOutputStream(aesFileBis);
byte[] b = new byte[8];
int i = cis.read(b);
while (i != -1) {
fos.write(b, 0, i);
i = cis.read(b);
}
fos.flush();
fos.close();
cis.close();
fis.close();
} catch(Exception e){
e.printStackTrace();
}
}
public static File retrieve(String args[]){
if (args.length!=1) {
System.out.println("Usage: UrlRetriever <URL>");
System.exit(-1);
}
File file = new File("Downloaded Encrypted Web Page.html");
try {
URL url=new URL(args[0]);
BufferedInputStream buffer=new
BufferedInputStream(url.openStream());
DataInputStream in= new DataInputStream(buffer);
String line;
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
while ((line=in.readLine())!=null){
bw.write(line);
}
bw.close();
in.close();
} catch(MalformedURLException mue) {
System.out.println(args[0]+"is an invalid URL:"+mue);
}catch(IOException ioe) {
System.out.println("IOException: "+ioe);
}
return file;
}
}
You can download my work out HERE
Saturday, November 24, 2012
Very Simple My Own Shell in C
Do you want to publish source codes in your blog or web site as follows.
Visit Source Code Formatter
Visit Source Code Formatter
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char buf[256]={0};
printf("\n\n\n");
while(1){
printf("\nMOSH : ");
if (fgets(buf, sizeof(buf), stdin) == NULL){
fprintf(stderr, "Invalid input command!\n");
exit(-1);
}
buf[strlen(buf)-1]='\0';
system(buf);
}
return 0;
}
Saturday, November 10, 2012
Maximum matching in bipartite graph using Hopcroft Karp algorithm in C ++
Do you want to publish source codes in your blog or web site as follows.
Visit Source Code Formatter
Visit Source Code Formatter
/*******************************************************/
/* 2nd Takehome Assignment */
/*Instructions : */
/* Data should be input to the program using text */
/* file (EX: test.txt) */
/* INPUT FORMAT: */
/* 4 5 5 */
/* 1 8 */
/* 1 5 */
/* 2 6 */
/* 3 9 */
/* 4 7 */
/* first three integers (n,m and e) */
/* n: number of left hand nodes */
/* m: number of right hand node */
/* e: number of incedent edges */
/* next integer(g[u][v]) */
/* g[u][v]: adjecent verteces */
/*******************************************************/
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <fstream>
using namespace std;
#define MAX 10000 //maximum allowed couples
#define NIL 0
#define INF (1<<28) //infinity
vector<int> G[MAX]; // G[0]= NIL u G1[G[1---n]] u G2[G[n+1---n+m]]
int n,m,e,match[MAX],dist[MAX];
//Breadth first search
bool bfs() {
int i, u, v, len;
queue<int> Q; //an integer queue
for(i=1; i<=n; i++) {
if(match[i]==NIL) { //i is not matched
dist[i] = 0;
Q.push(i);
}
else dist[i] = INF;
}
dist[NIL] = INF;
while(!Q.empty()) {
u = Q.front();
Q.pop();
if(u!=NIL) {
len = G[u].size();
for(i=0; i<len; i++) {
v = G[u][i];
if(dist[match[v]]==INF) {
dist[match[v]] = dist[u] + 1;
Q.push(match[v]);
}
}
}
}
return (dist[NIL]!=INF);
}
//depth first search
bool dfs(int u) {
int i, v, len;
if(u!=NIL) {
len = G[u].size();
for(i=0; i<len; i++) {
v = G[u][i];
if(dist[match[v]]==dist[u]+1) {
if(dfs(match[v])) {
match[v] = u;
match[u] = v;
return true;
}
}
}
dist[u] = INF;
return false;
}
return true;
}
int hopcroft_karp() {
int matching = 0, i;
// match[] is assumed NIL for all vertex in G
while(bfs())
for(i=1; i<=n; i++)
if(match[i]==NIL && dfs(i))
matching++;
return matching;
}
//read data file in to an array
int openDataFile(char *filename){
int A, B;
ifstream infile(filename);
if (!infile) { //fill not found
printf("There was a problem opening file %s for reading.\n",filename);
return 0;
}
printf("Opened %s for reading.\n", filename); //file opened
infile >> n >> m >>e; // for perfect matching n=m
while (infile >> A >> B) { //read adjecent verteces
G[A].push_back(B);
}
}
//main method starts here
int main() {
printf("\n*******************************\n\tFind Stable Cuples\n*******************************\n");
char filename[256] = {0};
int NumOfMatches,i;
printf("Enter data file name : ");
scanf("%s",&filename);
openDataFile(filename);
while(1){
int choice;
printf("\n*******************************\n\tFind Stable Cuples\n*******************************\n");
printf("Enter 1 : Find Optimal Solution\n");
printf("Enter 2 : Add New Data File\n");
printf("Enter 0 : Exit\n");
printf("\nEnter :");
scanf("%d",&choice);
if(choice==1){
openDataFile(filename);
NumOfMatches=hopcroft_karp(); //find matching perfect matching couples
printf("Number of couples matched : %d\n",NumOfMatches);
for(i=1;i<=n;i++){
printf("%d matched with %d\n",i,match[i]);
}
}else if(choice==2){
printf("Enter data file name : ");
scanf("%s",&filename);
}
else if(choice==0){
printf("Program Terminated\n");
exit(0);
}
else{
printf("Error : Wrong Input\n");
}
}
return 0;
}
Maximum matching in bipartite graph using Ford Fulkerson algorithm in C
Do you want to publish source codes in your blog or web site as follows.
Visit Source Code Formatter
Visit Source Code Formatter
/*******************************************************/
/* 2nd Takehome Assignment */
/* Maximum matching marriage couples using */
/* Ford Fulkerson Algorithm */
/*Instructions : */
/* Data should be input to the program using text */
/* file (EX: test.txt) */
/* INPUT FORMAT: */
/* 10 10 */
/* 1 6 */
/* 1 7 */
/* 2 7 */
/* 3 6 */
/* 3 8 */
/* 3 10 */
/* 4 7 */
/* 4 10 */
/* 5 7 */
/* 5 9 */
/* first two integers (n and e) */
/* n: number of nodes in the graph */
/* e: number of incedent edges */
/* next integer(u,v) */
/* g[u][v]: adjecent verteces */
/*******************************************************/
#include <stdio.h>
#include <stdlib.h>
// Basic Definitions
#define WHITE 0 //nodes status
#define GRAY 1
#define BLACK 2
#define MAX_NODES 1000 //maximum allowed nodes
#define INFINITY 1000000000
// variable Declarations
int n; // number of nodes
int e; // number of edges
int capacity[MAX_NODES][MAX_NODES]; // capacity matrix
int flow[MAX_NODES][MAX_NODES]; // flow matrix
int color[MAX_NODES]; // store node status
int parent[MAX_NODES]; // array to store augmenting path
int min (int x, int y) {
return x<y ? x : y; // returns minimum of x and y
}
// A Queue for Breadth-First Search
int head,tail;
int q[MAX_NODES+2];
void enqueue (int x) { //add to queue
q[tail] = x;
tail++;
color[x] = GRAY;
}
int dequeue () { //remove from queue
int x = q[head];
head++;
color[x] = BLACK;
return x;
}
// Breadth-First Search for an augmenting path
int bfs (int start, int target) {
int u,v;
for (u=0; u<n+2; u++) { //initialize node status
color[u] = WHITE;
}
head = tail = 0;
enqueue(start);
parent[start] = -1;
while (head!=tail) {
u = dequeue();
// Search all adjacent white nodes v. If the capacity
// from u to v in the residual network is positive,
// enqueue v.
for (v=0; v<n+2; v++) {
if (color[v]==WHITE && capacity[u][v]-flow[u][v]>0) {
enqueue(v);
parent[v] = u;
}
}
}
// If the color of the target node is black now,
// it means that we reached it.
//printf("%d\n",color[target]);
return color[target]==BLACK;
}
// Ford-Fulkerson Algorithm
int max_flow (int source, int sink) {
int i,j,u;
// Initialize empty flow.
int max_flow = 0;
for (i=0; i<n+2; i++) {
for (j=0; j<n+2; j++) {
flow[i][j] = 0;
}
}
// While there exists an augmenting path,
// increment the flow along this path.
printf("\nAugmented Path :\n");
while (bfs(source,sink)) {
// Determine the amount by which we can increment the flow.
int increment = INFINITY;
for (u=sink; parent[u]!=(-1); u=parent[u]) {
increment = min(increment,capacity[parent[u]][u]-flow[parent[u]][u]);
}
//printf("\n%d\n",increment);
// Now increment the flow.
for (u=sink; parent[u]!=(-1); u=parent[u]) {
flow[parent[u]][u] += increment;
flow[u][parent[u]] -= increment; // Reverse in residual
}
printf("\t");
// Path trace
for (u=sink; parent[u]!=(-1); u=parent[u]) {
printf("%d<-",u);
}
printf("%d adds %d incremental flow\n",source,increment);
max_flow += increment;
//printf("\n\n%d %d\n\n",source,sink);
}
//printf("%d\n",s);
// No augmenting path anymore. We are done.
return max_flow;
}
// Reading the input file and the main program
int read_input_file() {
int a,b,i,j;
char filename[256] = {0};
printf("Enter data file name : ");
scanf("%s",&filename);
FILE* input = fopen(filename,"r");
if(!input){
printf("There was a problem opening file %s for reading.\n",filename);
return 0;
}
// read number of nodes and edges
fscanf(input,"%d %d",&n,&e);
// initialize empty capacity matrix
for (i=0; i<n+2; i++) {
for (j=0; j<n+2; j++) {
capacity[i][j] = 0;
}
}
// read edge capacities
for (i=0; i<e; i++) {
fscanf(input,"%d %d",&a,&b);
if(a==b){
printf("ERROR : Graph is not bipartite");
return 0;
}
capacity[a][b]= 1; // Could have parallel edges
}
fclose(input);
for(i=0;i<n/2;i++){
capacity[0][i+1]= 1;
capacity[i+6][n+1]= 1;
}
return 1;
}
//Extra works
/*void read_input_file_w() {
int a,b,c,i,j;
FILE* input = fopen(filename,"a+");
// read number of nodes and edges
fscanf(input,"%d %d",&n,&e);
// initialize empty capacity matrix
for (i=0; i<n+2; i++) {
for (j=0; j<n+2; j++) {
capacity[i][j] = 0;
}
}
// read edge capacities
for (i=0; i<e; i++) {
fscanf(input,"%d %d",&a,&b);
capacity[b][a]= 1; // Could have parallel edges
}
fclose(input);
for(i=0;i<n/2;i++){
capacity[0][i+6]= 1;
capacity[i+1][n+1]= 1;
}
}
void printMatrix(){
int i,j;
for (i=0; i<=n+1; i++) {
for (j=0; j<=n+1; j++) {
printf("%d ",capacity[i][j]);
}
printf("\n");
}
}*/
int start(){
int isBipartite=0;
while(!isBipartite){
printf("\n*******************************\n\tFind Stable Cuples\n*******************************\n");
isBipartite=read_input_file();
//printMatrix();
}
return isBipartite;
}
int main () {
int i,j;
int isBipartite=0;
isBipartite=start();
while(isBipartite){
int choice;
printf("\n*******************************\n\tFind Stable Cuples\n*******************************\n");
printf("Enter 1 : Find Optimal Solution\n");
printf("Enter 2 : Add New Data File\n");
printf("Enter 0 : Exit\n");
printf("\nEnter :");
scanf("%d",&choice);
if(choice==1){
printf("\ntotal flow is %d\n",max_flow(0,n+1));
printf("\nMatched couples:\n");
for (i=1; i<=n; i++)
//printf("%d ",parent[i]);
for (j=1; j<=n; j++)
if (flow[i][j]>0)
printf("\tMen:%d matched with Women:%d\n",i,j);
}else if(choice==2){
isBipartite=start();
//printMatrix();
}
else if(choice==0){
printf("Program Terminated\n");
exit(0);
}
else{
printf("Error : Wrong Input\n");
}
}
return 0;
}
Sunday, October 21, 2012
Rabin Carp String Matching Algorithm
Rabin Carp Algorithm is also one of the string matching algorithm. This algorithm is also an improved version of Naive or Brute Force String Matching Algorithm. Because this algorithm is the a very basic sub-string matching algorithm, but it’s good for some reasons. For example it doesn’t require preprocessing of the text or the pattern. The problem is that it’s very slow. That is why in many cases brute force matching can’t be very useful.
Michael O. Rabin and Richard M. Karp came up with the idea of hashing the pattern and to check it against a hashed sub-string from the text in 1987.Rabin Carp algorithm is one of the better string matching algorithm than Brute Force algorithm. This algorithm avoid comparison of every character of pattern with characters of text in each position. To do that this algorithm uses hashing. Hashing is the process of converting our data in to numerical value. To convert data in to numerical value we should have assign a numerical value for each text in the alphabet and using any hashing functions we can calculate hash value of any pattern.
As an example if our pattern P=AABCB and we get ascii values of characters.
Then ascii value of A=65,B=66,C=67 and we can get hash value of above pattern using any hashing functions.
Hash function 1
hash value h(P) = 65+65+66+67+66 = 329
Hash function 2
hash value h(P) = 65*1+65*2+66*3+67*4+66*5 =991
Like above you can use any hash functions to calculate hash values.
We can say that if two strings are equal, then hash values of these two string must be same. But if hash values of two strings are equal then we can't say these two strings are equal. I may be or not. This is the basic idea of the Rabin Carp algorithm.
Algorithm
Algorithm
Compute hash value of pattern[h(P)]
Compute hash value of sub string of text[h(t)]
If h(P)=h(t)
Compare pattern and sub string character by character
If mismatch found
move by one position and go to second step
Else
matching sub string found in the text
Else
move by one position and go to second step
Lets learn the algorithm using an example.
Alphabet S = {A,B,C,D,E,F,G,H}
Text T = AABDCFFACGABBCABHGADEAADG
Pattern P = BBCABHG
Here i am going to assign value of character in the alphabet instead of assigning ascii values.
Value(A) = 1
Value(B) = 2
Value(C) = 3
Value(D) = 4
Value(E) = 5
Value(F) = 6
Value(G) = 7
Value(H) = 8
Then hash value of pattern h(P) = 2+2+3+1+2+8+7 = 25

Alphabet S = {A,B,C,D,E,F,G,H}
Text T = AABDCFFACGABBCABHGADEAADG
Pattern P = BBCABHG
Here i am going to assign value of character in the alphabet instead of assigning ascii values.
Value(A) = 1
Value(B) = 2
Value(C) = 3
Value(D) = 4
Value(E) = 5
Value(F) = 6
Value(G) = 7
Value(H) = 8
Then hash value of pattern h(P) = 2+2+3+1+2+8+7 = 25

Hash value of [AABDCFF] is 23. Hash values are not equal. Sub string is not matched. Move by one position.
Hash value of [ABDCFFA] is 23. Hash values are not equal. Sub string is not matched. Move by one position.
Hash value of [BDCFFAC] is 25. Hash values are equal. Sub string may be matched. Compare pattern with sub string in the text.
First letter B matched with sub string. But second one mismatched. Move by one position.
Hash value of [DCFFACG] is 29. Hash values are not equal. Sub string is not matched. Move by one position.
Hash value of [CFFACGA] is 26. Hash values are not equal. Sub string is not matched. Move by one position.
Hash value of [FFACGAB] is 25. Hash values are equal. Sub string may be matched. Compare pattern with sub string in the text.
First letter B mismatched. Move by one position. Like these you can find matching sub string.
C implementation of Rabin Carp Algorithm
Note that the Rabin-Karp algorithm also needs O(m) preprocessing time.
C implementation of Rabin Carp Algorithm
Complexity
The Rabin-Karp algorithm has the complexity of O(nm) where n, of course, is the length of the text, while m is the length of the pattern. So where it is compared to brute-force matching? Well, brute force matching complexity is O(nm), so as it seems there’s no much gain in performance. However it’s considered that Rabin-Karp’s complexity is O(n+m) in practice, and that makes it a bit faster, as shown on the chart below.
Rabin-Karp's complexity is O(nm), but in practice it's O(n+m)!
Advantages
- Not faster than brute force matching in theory, but in practice its complexityis O(n+m)
- Good hashing function it can be quite effective and it’s easy to implement!
- Multiple pattern matching support
- Good for plagiarism, because it can deal with multiple pattern matching!
Disadvantages
- There are lots of string matching algorithms that are faster than O(n+m)
- It’s practically as slow as brute force matching and it requires additional space
Rabin-Karp is a great algorithm for one simple reason – it can be used to match against multiple pattern. This makes it perfect to detect plagiarism even for larger phrases.
Subscribe to:
Posts (Atom)
















