├── .project ├── LICENSE ├── README.md ├── arithmeticExpressions ├── infixEvaluation │ ├── InfixEvaluation.cpp │ └── InfixEvaluation_Input.txt ├── infixToPostfix │ ├── InfixToPostfix.cpp │ └── InfixToPostfix_Input.txt ├── infixToPrefix │ ├── InfixToPrefix.cpp │ └── InfixToPrefix_Input.txt ├── postfixEvaluation │ ├── PostfixEvaluation.cpp │ └── PostfixEvaluation_Input.txt └── prefixEvaluation │ ├── PrefixEvaluation.cpp │ └── PrefixEvaluation_Input.txt ├── dataStructures ├── listImplementation │ ├── ListImplementation_Input.txt │ ├── OneWayLinkedList.cpp │ ├── TwoWayLinkedList.cpp │ └── implementationUsingNode │ │ ├── ListImplementation_Input.txt │ │ ├── OneWayLinkedList.cpp │ │ └── TwoWayLinkedList.cpp ├── stack │ ├── StackImplementation.asm │ └── StackImplementation.c ├── string │ ├── String.c │ ├── StringLongestCommonSubSecuence.cpp │ └── StringLongestIncreasingSubSecuence.cpp └── tree │ ├── BinaryHeap.cpp │ ├── BinomialTree.cpp │ ├── activitySelection │ ├── ActivitySelection.cpp │ └── ActivitySelection_Input.txt │ ├── balanceTree │ ├── BalanceTree.java │ └── Touple.java │ └── kdTree │ └── KdTreeImpl.java ├── graph ├── breathFirstSearch │ ├── BreadthFirstSearch.cpp │ ├── BreadthFirstSearch_Input.PNG │ ├── BreadthFirstSearch_Input.txt │ ├── BreadthFirstSearch_Input2.PNG │ └── BreadthFirstSearch_Input2.txt ├── depthFirstSearch │ ├── DepthFirstSearch.cpp │ ├── DepthFirstSearch_Input.PNG │ └── DepthFirstSearch_Input.txt ├── maxFlow │ ├── MaxFlow.cpp │ ├── MaxFlow_Input.png │ └── MaxFlow_Input.txt ├── shortestPathAlgorithm │ ├── ShortestPathAlgorithm.cpp │ ├── ShortestPathAlgorithm_Input.PNG │ ├── ShortestPathAlgorithm_Input.txt │ ├── ShortestPathAlgorithm_Input2.PNG │ └── ShortestPathAlgorithm_Input2.txt └── topologicalSort │ ├── TopologicalSort.cpp │ ├── TopologicalSort_Input.PNG │ └── TopologicalSort_Input.txt ├── numericalMethods ├── calculus │ ├── DerivetivesOnDifferentPoints.c │ ├── IntegrationForwardInterpolation.c │ ├── IntegrationSimpsonRule.c │ └── IntersectingArea.c └── equationSolving │ ├── LinearEquationSolvingProcess.c │ └── NonLinearEquationSolvingProcess.c ├── others ├── cigaretteSmokersProblem │ ├── Agent.java │ ├── CigaretteSmokersProblemMain.java │ ├── Graph.java │ └── Smoker.java ├── geneticAlgorithm │ ├── Board.java │ └── Player.java └── huffmanAlgorithm │ ├── Extractor.cpp │ ├── ZipMaker.cpp │ └── huffmanAlgorithm_Input.txt ├── playWithNumbers ├── factorial │ ├── BigFactorials.c │ ├── DigitsOfFactorial.c │ └── FactorsOfFactorial.c ├── fibonaciiNumber │ ├── FibonaciiNumber.c │ ├── FibonaciiSerise.c │ └── FibonaciiSum.c ├── otherNumbers │ ├── FriendNumbers.c │ └── PerfectNumberHaunting.c ├── pascalTriangle │ ├── BetterPascalTriangle.c │ ├── EasyPascalTriangle.c │ ├── PascalTriangle.c │ ├── Piramid.c │ └── RealPascalTriangle.c ├── primeNumber │ ├── AnotherWayOfPrimeNumberHaunting.c │ ├── DefinitePrimeNumberHaunting.c │ ├── Factors.c │ ├── GreatPrimeNumberHaunting.c │ ├── GreatestPrimeNumberHaunting.c │ ├── PrimeNumber.c │ ├── PrimeNumberHaunting.asm │ └── PrimeNumberHaunting.c └── time │ ├── AgeCalculator.c │ ├── LeapYearCounter.c │ ├── Stopwatch.c │ └── TimeAfter.c ├── search ├── aStarSearch │ └── AStarSearch.java ├── binarySearch │ ├── BinarySearch.cpp │ └── BinarySearch_Input.txt └── knuthMorrisPrattAlgorithm │ ├── KnuthMorrisPrattAlgorithm.cpp │ └── KnuthMorrisPrattAlgorithm_Input.txt ├── sort └── quickSort │ ├── QuickSort.cpp │ └── QuickSort_Input.txt └── thread ├── Input ├── priorityBased ├── nonpremitive │ ├── CPU.java │ └── PriorityBasedNonpremitive.java └── premitive │ ├── CPU.java │ └── PriorityBasedPremitive.java ├── shortestJob ├── nonpremitive │ ├── CPU.java │ └── ShortestJobFirstNonpremitive.java └── premitive │ ├── CPU.java │ └── ShortestJobFirstPremitive.java └── utilClasses ├── Graph.java ├── Process.java └── Result.java /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | AlgorithmImplementations 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2015 Minhas Kamal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /arithmeticExpressions/infixEvaluation/InfixEvaluation.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Name: Minhas Kamal (minhaskamal024@gmail.com) 3 | Description: This program reads an infix notation from a file & finds the result. 4 | Date: Oct-2013 5 | **/ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | 16 | double stringToDouble(string str); //converts a string to a double 17 | int operatorPrecedence(char operat); //returns bigger integer depending on the precedence 18 | double operation(double a, double b, char operat); //returns a result depending on the operator 19 | 20 | 21 | struct element{ 22 | string str; //string property 23 | double value; //value of the string 24 | bool opert; //operator-true, operand-false 25 | }; 26 | 27 | int main(){ 28 | ifstream input; //the input file 29 | 30 | input.open("InfixEvaluation_Input.txt"); 31 | if(!input.is_open()){ //when file is not found exit 32 | cout << "File name is wrong!\n"; 33 | return 0; 34 | } 35 | 36 | string s; //stores the input file's element as string 37 | vector inputElement; //holds the behaviour of the input elements 38 | while(input >> s){ //the loop determines the behaviour of the input elements 39 | element elem; 40 | elem.str = s; 41 | 42 | if(s[0]=='+' || s[0]=='-' || s[0]=='*' || s[0]=='/' || s[0]=='^' 43 | || s[0]=='(' || s[0]==')'){ //when an operator is found 44 | elem.value = -0.0; 45 | elem.opert = true; 46 | } 47 | else{ 48 | elem.value = stringToDouble(s); //this method converts string to double 49 | elem.opert = false; 50 | } 51 | 52 | inputElement.push_back(elem); 53 | } 54 | 55 | input.close(); //closing the stream 56 | 57 | /*cout << "string \toperat \tvalue \n"; //output of the inputElement 58 | for(int i=0; i operand; //postfix form of the input 71 | stack operat; //temporarily stores operators 72 | 73 | operat.push('('); //pushing '(' at the beginning of stack 74 | 75 | bool everyThingOkFlag=true; 76 | 77 | for(int i=0; !operat.empty(); i++){ //evaluation 78 | if(!inputElement[i].opert){ 79 | operand.push(inputElement[i].value); 80 | } 81 | else{ 82 | if(inputElement[i].str[0]=='('){ 83 | operat.push('('); 84 | } 85 | else if(inputElement[i].str[0]==')'){ 86 | while(operat.top()!='('){ 87 | double b = operand.top(); //taking first two elements of the stack 88 | operand.pop(); 89 | double a = operand.top(); 90 | operand.pop(); 91 | 92 | char oprt = operat.top(); //taking the top the operator of stack 93 | operat.pop(); 94 | 95 | double result = 0; //result after the operation of a & b 96 | result = operation(a, b, oprt); 97 | 98 | operand.push(result); //pushing the result in the operand stack 99 | } 100 | operat.pop(); //pops the '(' 101 | } 102 | else{ 103 | int opertPrecedence = operatorPrecedence(inputElement[i].str[0]); 104 | 105 | while(operat.top()!='('){ 106 | int stackPrecedence = operatorPrecedence(operat.top()); 107 | 108 | if(stackPrecedence < opertPrecedence) break; 109 | else{ 110 | double b = operand.top(); //taking first two elements of the stack 111 | operand.pop(); 112 | double a = operand.top(); 113 | operand.pop(); 114 | 115 | char oprt = operat.top(); //taking the top the operator of stack 116 | operat.pop(); 117 | 118 | double result = 0; //result after the operation of a & b 119 | result = operation(a, b, oprt); 120 | 121 | operand.push(result); //pushing the result in the operand stack 122 | } 123 | } 124 | operat.push(inputElement[i].str[0]); 125 | } 126 | } 127 | 128 | if(i>inputElement.size()) { 129 | cout << "Math error! \n"; 130 | everyThingOkFlag=false; 131 | break; 132 | } 133 | } 134 | 135 | if(everyThingOkFlag) cout << "The result is: " << operand.top() << endl; 136 | 137 | return 0; 138 | } 139 | 140 | 141 | double stringToDouble(string str){ //converts a string to a double 142 | double value=0, //holds the value of the string 143 | afterPoint=0; //holds the value after a point 144 | 145 | bool flag = false; //represents if any point is found in the string 146 | int numberOfElementAfterPoint=0; 147 | 148 | for(int i=0; str[i]!=NULL; i++){ 149 | if(str[i]=='.') flag = true; //waiting for a '.' 150 | else{ 151 | if(flag){ 152 | afterPoint = afterPoint*10 + (str[i]-48); 153 | numberOfElementAfterPoint++; 154 | } 155 | else 156 | value = value*10 + (str[i]-48); 157 | } 158 | } 159 | 160 | int divisor=1; //divides the afterPoint element by precise number of 10s 161 | for(int i=0; i 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | 15 | double stringToDouble(string str); 16 | int operatorPrecedence(string str); 17 | struct element{ 18 | string str; //string property 19 | double value; //value of the string 20 | bool opert; //operator-true, operand-false 21 | }; 22 | 23 | int main(){ 24 | ifstream input; //the input file 25 | 26 | input.open("InfixToPostfix_Input.txt"); 27 | if(!input.is_open()){ //when file is not found exit 28 | cout << "File name is wrong!\n"; 29 | return 0; 30 | } 31 | 32 | string s; //stores the input file's element as string 33 | vector inputElement; //holds the behaviour of the input elements 34 | while(input >> s){ //the loop determines the behaviour of the input elements 35 | element elem; 36 | elem.str = s; 37 | 38 | if(s[0]=='+' || s[0]=='-' || s[0]=='*' || s[0]=='/' || s[0]=='^' || s[0]=='(' || s[0]==')'){ //when an operator is found 39 | elem.value = -0.0; 40 | elem.opert = true; 41 | } 42 | else{ 43 | elem.value = stringToDouble(s); //this method converts string to double 44 | elem.opert = false; 45 | } 46 | 47 | inputElement.push_back(elem); 48 | } 49 | 50 | input.close(); //closing the stream 51 | 52 | /*cout << "string \toperat \tvalue \n"; //output of the inputElement 53 | for(int i=0; i postfix; //postfix form of the input 66 | stack tempOpertStore; //temporarily stores operators 67 | 68 | tempOpertStore.push("("); 69 | for(int i=0; !tempOpertStore.empty(); i++){ //conversion 70 | if(!inputElement[i].opert){ 71 | postfix.push_back(inputElement[i].str); 72 | } 73 | else{ 74 | if(inputElement[i].str[0]=='('){ 75 | tempOpertStore.push(inputElement[i].str); 76 | } 77 | else if(inputElement[i].str[0]==')'){ 78 | while(tempOpertStore.top()[0]!='('){ 79 | postfix.push_back(tempOpertStore.top()); 80 | tempOpertStore.pop(); 81 | } 82 | tempOpertStore.pop(); //pops the '(' 83 | } 84 | else{ 85 | int opertPrecedence = operatorPrecedence(inputElement[i].str); 86 | 87 | while(tempOpertStore.top()[0]!='('){ 88 | int stackPrecedence = operatorPrecedence(tempOpertStore.top()); 89 | 90 | if(stackPrecedence < opertPrecedence) break; 91 | else{ 92 | postfix.push_back(tempOpertStore.top()); 93 | tempOpertStore.pop(); 94 | } 95 | } 96 | tempOpertStore.push(inputElement[i].str); 97 | } 98 | } 99 | 100 | /*cout << "after " << inputElement[i].str << ": "; //analysing every step 101 | for(int j=0; j 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | 15 | double stringToDouble(string str); 16 | int operatorPrecedence(string str); 17 | struct element{ 18 | string str; //string property 19 | double value; //value of the string 20 | bool opert; //operator-true, operand-false 21 | }; 22 | 23 | int main(){ 24 | ifstream input; //the input file 25 | 26 | input.open("InfixToPrefix_Input.txt"); 27 | if(!input.is_open()){ //when file is not found exit 28 | cout << "File name is wrong!\n"; 29 | return 0; 30 | } 31 | 32 | string s; //stores the input file's element as string 33 | vector inputElement; //holds the behaviour of the input elements 34 | 35 | element e; //pushing '(' at the start of the input representing the start 36 | e.str = "("; 37 | e.opert = true; 38 | e.value = -0.0; 39 | inputElement.push_back(e); 40 | 41 | while(input >> s){ //the loop determines the behaviour of the input elements 42 | element elem; 43 | elem.str = s; 44 | 45 | if(s[0]=='+' || s[0]=='-' || s[0]=='*' || s[0]=='/' || s[0]=='^' || s[0]=='(' || s[0]==')'){ //when an operator is found 46 | elem.value = -0.0; 47 | elem.opert = true; 48 | } 49 | else{ 50 | elem.value = stringToDouble(s); //this method converts string to double 51 | elem.opert = false; 52 | } 53 | 54 | inputElement.push_back(elem); 55 | } 56 | 57 | input.close(); //closing the stream 58 | 59 | /*/cout << "string \toperat \tvalue \n"; //output of the inputElement 60 | for(int i=0; i prefix; //postfix form of the input 65 | stack tempOpertStore; //temporarily stores operators 66 | 67 | tempOpertStore.push(")"); 68 | 69 | for(int i=inputElement.size()-1; !tempOpertStore.empty(); i--){ //conversion 70 | if(!inputElement[i].opert){ 71 | prefix.push_back(inputElement[i].str); 72 | } 73 | else{ 74 | if(inputElement[i].str[0]==')'){ 75 | tempOpertStore.push(inputElement[i].str); 76 | } 77 | else if(inputElement[i].str[0]=='('){ 78 | while(tempOpertStore.top()[0]!=')'){ 79 | prefix.push_back(tempOpertStore.top()); 80 | tempOpertStore.pop(); 81 | } 82 | tempOpertStore.pop(); //pops the ')' 83 | } 84 | else{ 85 | int opertPrecedence = operatorPrecedence(inputElement[i].str); 86 | 87 | while(tempOpertStore.top()[0]!=')'){ 88 | int stackPrecedence = operatorPrecedence(tempOpertStore.top()); 89 | 90 | if(stackPrecedence < opertPrecedence) break; 91 | else{ 92 | prefix.push_back(tempOpertStore.top()); 93 | tempOpertStore.pop(); 94 | } 95 | } 96 | tempOpertStore.push(inputElement[i].str); 97 | } 98 | } 99 | 100 | /*cout << "after " << inputElement[i].str << ": "; //analysing every step 101 | for(int j=0; j=0; i--) //the output 110 | cout << prefix[i] << " " ; 111 | cout << "\n\n"; 112 | 113 | 114 | 115 | return 0; 116 | } 117 | 118 | 119 | double stringToDouble(string str){ //converts a string to a double 120 | double value=0, //holds the value of the string 121 | afterPoint=0; //holds the value after a point 122 | 123 | bool flag = false; //represents if any point is found in the string 124 | int numberOfElementAfterPoint=0; 125 | 126 | for(int i=0; str[i]!=NULL; i++){ 127 | if(str[i]=='.') flag = true; 128 | else{ 129 | if(flag){ 130 | afterPoint = afterPoint*10 + (str[i]-48); 131 | numberOfElementAfterPoint++; 132 | } 133 | else 134 | value = value*10 + (str[i]-48); 135 | } 136 | } 137 | 138 | int divisor=1; //divides the afterPoint element by precise number of 10s 139 | for(int i=0; i 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | 15 | double stringToDouble(string str); 16 | double exponent(double a, double b); 17 | 18 | struct element{ 19 | string str; //string property 20 | double value; //value of the string 21 | bool opert; //operator-true, operand-false 22 | }; 23 | 24 | 25 | int main(){ 26 | ifstream input; //the input file 27 | 28 | input.open("PostfixEvaluation_Input.txt"); 29 | if(!input.is_open()){ //when file is not found exit 30 | cout << "File name is wrong!\n"; 31 | return 0; 32 | } 33 | 34 | 35 | string s; //stores the input file's element as string 36 | vector inputElement; //holds the behaviour of the input elements 37 | while(input >> s){ //the loop determines the behaviour of the input elements 38 | element elem; 39 | elem.str = s; 40 | 41 | if(s[0]=='+' || s[0]=='-' || s[0]=='*' || s[0]=='/' || s[0]=='^' || s[0]=='(' || s[0]==')'){ //when an operator is found 42 | elem.value = -0.0; 43 | elem.opert = true; 44 | } 45 | else{ 46 | elem.value = stringToDouble(s); //this method converts string to double 47 | elem.opert = false; 48 | } 49 | 50 | inputElement.push_back(elem); 51 | } 52 | 53 | input.close(); //closing the stream 54 | 55 | 56 | /*cout << "string \toperat \tvalue \n"; //output of the inputElement 57 | for(int i=0; i tempOperndStore; //here operands are stored temporarily 62 | 63 | for(int i=0; i 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | 15 | double stringToDouble(string str); 16 | double exponent(double a, double b); 17 | 18 | struct element{ 19 | string str; //string property 20 | double value; //value of the string 21 | bool opert; //operator-true, operand-false 22 | }; 23 | 24 | 25 | int main(){ 26 | ifstream input; //the input file 27 | 28 | input.open("PrefixEvaluation_Input.txt"); 29 | if(!input.is_open()){ //when file is not found exit 30 | cout << "File name is wrong!\n"; 31 | return 0; 32 | } 33 | 34 | 35 | string s; //stores the input file's element as string 36 | vector inputElement; //holds the behaviour of the input elements 37 | 38 | element el; //pushing a '(' to determine the start 39 | el.opert=true; 40 | el.str="("; 41 | el.value=-0; 42 | inputElement.push_back(el); 43 | 44 | 45 | while(input >> s){ //the loop determines the behaviour of the input elements 46 | element elem; 47 | elem.str = s; 48 | 49 | if(s[0]=='+' || s[0]=='-' || s[0]=='*' || s[0]=='/' || s[0]=='^' || s[0]=='(' || s[0]==')'){ //when an operator is found 50 | elem.value = -0.0; 51 | elem.opert = true; 52 | } 53 | else{ 54 | elem.value = stringToDouble(s); //this method converts string to double 55 | elem.opert = false; 56 | } 57 | 58 | inputElement.push_back(elem); 59 | } 60 | 61 | input.close(); //closing the stream 62 | 63 | 64 | /*cout << "string \toperat \tvalue \n"; //output of the inputElement 65 | for(int i=0; i tempStore; //here operands are stored temporarily 70 | 71 | for(int i=0; i 7 | #include 8 | 9 | using namespace std; 10 | 11 | list creatList(); //creates the list 12 | void traversing(list myList); //prints all the integers in the list 13 | int searching(list myList, int searchingElement); //searches for selected element 14 | list deleting(list myList, int deletingElement, int a); //deleting selected element 15 | list inserting(list myList, int destinationElement, int insertingElement, int a); //inserts certain element in definite place 16 | list destroy(list myList); //destroying the list 17 | 18 | 19 | int main() 20 | { 21 | list myList; 22 | myList = creatList(); 23 | 24 | while(true) 25 | { 26 | int selection; //user action 27 | cout << "\n##What do you want to do? \npress- 1 for traversing\n"; //user prompt 28 | cout << " 2 for searching\n 3 for deleting\n 4 for inserting\n"; 29 | cout << " 5 for exit\nSelection: "; 30 | cin >> selection; 31 | 32 | 33 | if(selection==1) 34 | traversing(myList); 35 | 36 | else if(selection==2) 37 | { 38 | int searchingElement; //the element to be searched 39 | cout << "Enter the element you want to search for: "; 40 | cin >> searchingElement; 41 | 42 | cout << "\n* " << searchingElement << " is found " << searching(myList, searchingElement) << " times.\n"; 43 | } 44 | 45 | else if(selection==3) 46 | { 47 | int deletingElement; //the element to be deleted 48 | cout << "Enter the element: "; 49 | cin >> deletingElement; 50 | 51 | int a=0; //determines the correct element 52 | cout << "Press 1 for deleting " << deletingElement; 53 | cout << "\n 2 for deleting the element after " << deletingElement << "\nSelection- "; 54 | cin >> a; 55 | 56 | myList = deleting(myList, deletingElement, a); 57 | } 58 | 59 | else if(selection==4) 60 | { 61 | int insertingElement, destinationElement; //the element to be deleted 62 | cout << "Enter the element: "; 63 | cin >> insertingElement; 64 | cout << "Enter the destination element: "; 65 | cin >> destinationElement; 66 | 67 | int a=0; //determines the correct element 68 | cout << "Press 1 for inserting after " << destinationElement; 69 | cout << "\n 2 for inserting before " << destinationElement << "\nSelection- "; 70 | cin >> a; 71 | 72 | myList = inserting(myList, destinationElement, insertingElement, a); 73 | } 74 | 75 | else if(selection==5) break; 76 | 77 | else cout << "#Invalid input!\n"; 78 | } 79 | myList = destroy(myList); 80 | } 81 | 82 | 83 | list creatList() //creates the list 84 | { 85 | list myList; //declaring the list 86 | int intNumber, //the number of integers 87 | input; //the input that the user gives 88 | 89 | cin >> intNumber; 90 | for(int i=0; i> input; 93 | myList.push_back(input); 94 | } 95 | return myList; 96 | } 97 | 98 | void traversing(list myList) //prints the List 99 | { 100 | list:: iterator it; //declaring a iterator 101 | 102 | cout << endl; 103 | for(it=myList.begin(); it!=myList.end(); it++) 104 | cout << *it << " "; 105 | cout << endl << endl ; 106 | } 107 | 108 | int searching(list myList, int searchingElement) 109 | { 110 | int found=0; //number of the searching element 111 | list:: iterator it; //declaring iterator 112 | 113 | for(it=myList.begin(); it!=myList.end(); it++) 114 | if(searchingElement==*it) found++; 115 | 116 | return found; 117 | } 118 | 119 | list deleting(list myList, int deletingElement, int a) 120 | { 121 | int flag=1; 122 | if(a==1) myList.remove(deletingElement); 123 | else { 124 | list:: iterator it; //declaring iterator 125 | flag=0; 126 | 127 | for(it=myList.begin(); it!=myList.end(); it++){ 128 | if(deletingElement==*it){ 129 | it++; 130 | it=myList.erase(it); 131 | flag=1; 132 | if(it==myList.end()) break; 133 | } 134 | } 135 | } 136 | 137 | if(flag) cout << "\n* Deleted!\n"; 138 | return myList; 139 | } 140 | 141 | list inserting(list myList, int destinationElement, int insertingElement, int a) 142 | { 143 | int flag=0; 144 | list:: iterator it; //declaring iterator 145 | 146 | for(it=myList.begin(); it!=myList.end(); it++){ 147 | if(destinationElement==*it) { 148 | flag=1; 149 | break; 150 | } 151 | } 152 | if(a==1) it++; 153 | myList.insert(it, insertingElement); 154 | 155 | if(flag) cout << "\n* Inserted!!\n"; 156 | else cout << "Destination not found.\n"; 157 | 158 | return myList; 159 | } 160 | 161 | list destroy(list myList) 162 | { 163 | myList.clear(); 164 | return myList; 165 | } 166 | 167 | 168 | -------------------------------------------------------------------------------- /dataStructures/listImplementation/TwoWayLinkedList.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Developer: Minhas Kamal (BSSE-0509, IIT, DU) 3 | * Date: 13.Sep.2013 4 | **/ 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | list creatList(); //creates the list 12 | void traversing(list myList, int choice); //prints all the integers in the list 13 | int searching(list myList, int searchingElement); //searches for selected element 14 | list deleting(list myList, int deletingElement, int selection); //deleting selected element 15 | list insertingAtBeginning(list myList, int insertingElement); //inserting at beginning 16 | list insertingAtEnd(list myList, int insertingElement); //inserting at end 17 | list inserting(list myList, int destinationElement, int insertingElement, int a); //inserts certain element in definite place 18 | list destroy(list myList); //destroying the list 19 | 20 | 21 | int main() 22 | { 23 | list myList; 24 | myList = creatList(); 25 | 26 | while(true) 27 | { 28 | int selection; //user action 29 | cout << "\n##What do you want to do? \npress- 1 for traversing\n"; //user prompt 30 | cout << " 2 for searching\n 3 for deleting\n 4 for inserting\n"; 31 | cout << " 5 for exit\nSelection: "; 32 | cin >> selection; 33 | 34 | 35 | if(selection==1){ 36 | int choice; 37 | cout << "How do you want to traverse?\npress- 1 for traversing first to last"; 38 | cout << "\n 2 for traversing last to first\nSelection: "; 39 | cin >> choice; 40 | traversing(myList, choice); 41 | } 42 | 43 | else if(selection==2) 44 | { 45 | int searchingElement; //the element to be searched 46 | cout << "Enter the element you want to search for: "; 47 | cin >> searchingElement; 48 | 49 | cout << "\n"; 50 | cout << "* " << searchingElement << " is found " << searching(myList, searchingElement) << " times.\n"; 51 | } 52 | 53 | else if(selection==3) 54 | { 55 | int deletingElement; //the element to be deleted 56 | cout << "Enter the element: "; 57 | cin >> deletingElement; 58 | 59 | int sel=0; //determines the correct element 60 | cout << "Press 1 for deleting " << deletingElement; 61 | cout << "\n 2 for deleting the element after " << deletingElement; 62 | cout << "\n 3 for deleting the element before " << deletingElement << "\nSelection- "; 63 | cin >> sel; 64 | 65 | myList = deleting(myList, deletingElement, sel); 66 | } 67 | 68 | else if(selection==4) 69 | { 70 | int insertingElement, destinationElement, sel; //the element to be deleted 71 | cout << "Enter the element: "; 72 | cin >> insertingElement; 73 | cout << "Where do you want to insert? \nPress 1 for beginning"; 74 | cout << "\n 2 for end\n 3 for chosing destination\nSelection: "; 75 | cin >> sel; 76 | 77 | if(sel==1) myList = insertingAtBeginning(myList, insertingElement); 78 | else if(sel==2) myList = insertingAtEnd(myList, insertingElement); 79 | else{ 80 | cout << "Enter the destination element: "; 81 | cin >> destinationElement; 82 | 83 | int a=0; //determines the correct element 84 | cout << "Press 1 for inserting after " << destinationElement; 85 | cout << "\n 2 for inserting before " << destinationElement << "\nSelection- "; 86 | cin >> a; 87 | 88 | myList = inserting(myList, destinationElement, insertingElement, a); 89 | } 90 | } 91 | 92 | else if(selection==5) break; 93 | 94 | else cout << "#Invalid input!\n"; 95 | } 96 | myList = destroy(myList); 97 | } 98 | 99 | 100 | list creatList() //creates the list 101 | { 102 | list myList; //declaring the list 103 | int intNumber, //the number of integers 104 | input; //the input that the user gives 105 | 106 | cin >> intNumber; 107 | for(int i=0; i> input; 110 | myList.push_back(input); 111 | } 112 | return myList; 113 | } 114 | 115 | void traversing(list myList, int choice) //prints the List 116 | { 117 | list:: iterator it; //declaring a iterator 118 | 119 | cout << endl; 120 | if(choice==1) 121 | for(it=myList.begin(); it!=myList.end(); it++) 122 | cout << *it << " "; 123 | else 124 | for(it=myList.end(); it!=myList.begin(); ){ 125 | it--; 126 | cout << *it << " "; 127 | } 128 | cout << endl << endl ; 129 | } 130 | 131 | int searching(list myList, int searchingElement) 132 | { 133 | int found=0; //number of the searching element 134 | list:: iterator it, //declaring iterator 135 | old=myList.begin(); //this will store old position 136 | 137 | for(it=myList.begin(); it!=myList.end(); it++){ 138 | if(searchingElement==*it){ 139 | it++; 140 | if(old!=myList.begin() && it!=myList.end()){ 141 | cout << "*between " << *old << " and " << *it << endl; 142 | } 143 | else if(old==myList.begin()) cout << "*at beginning\n"; 144 | else cout << "*at end\n"; 145 | it--; 146 | found++; 147 | } 148 | old=it; 149 | } 150 | return found; 151 | } 152 | 153 | list deleting(list myList, int deletingElement, int choice) 154 | { 155 | int flag=1; 156 | if(choice==1) myList.remove(deletingElement); 157 | else if(choice==2) { 158 | list:: iterator it; //declaring iterator 159 | flag=0; 160 | 161 | for(it=myList.begin(); it!=myList.end(); it++){ 162 | if(deletingElement==*it){ 163 | it++; 164 | it=myList.erase(it); 165 | flag=1; 166 | if(it==myList.end()) break; 167 | } 168 | } 169 | } 170 | else { 171 | list:: iterator it; //declaring iterator 172 | flag=0; 173 | 174 | for(it=myList.begin(); it!=myList.end(); it++){ 175 | if(deletingElement==*it){ 176 | it--; 177 | it=myList.erase(it); 178 | flag=1; 179 | if(it==myList.end()) break; 180 | } 181 | } 182 | } 183 | 184 | if(flag) cout << "\n* Deleted!\n"; 185 | return myList; 186 | } 187 | 188 | list insertingAtBeginning(list myList, int insertingElement) 189 | { 190 | myList.insert(myList.begin(), insertingElement); 191 | cout << "\n* Inserted!!\n"; 192 | return myList; 193 | } 194 | 195 | list insertingAtEnd(list myList, int insertingElement) 196 | { 197 | myList.insert(myList.end(), insertingElement); 198 | cout << "\n* Inserted!!\n"; 199 | return myList; 200 | } 201 | 202 | list inserting(list myList, int destinationElement, int insertingElement, int a) 203 | { 204 | int flag=0; 205 | list:: iterator it; //declaring iterator 206 | 207 | for(it=myList.begin(); it!=myList.end(); it++){ 208 | if(destinationElement==*it) { 209 | flag=1; 210 | break; 211 | } 212 | } 213 | if(a==1) it++; 214 | myList.insert(it, insertingElement); 215 | 216 | if(flag) cout << "\n* Inserted!!\n"; 217 | else cout << "Destination not found.\n"; 218 | 219 | return myList; 220 | } 221 | 222 | list destroy(list myList) 223 | { 224 | myList.clear(); 225 | return myList; 226 | } 227 | 228 | 229 | -------------------------------------------------------------------------------- /dataStructures/listImplementation/implementationUsingNode/ListImplementation_Input.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 3 | 12 4 | 5 | 45 6 | 7 | 98 8 | 9 | 65 10 | 11 | 31 12 | 13 | 54 14 | 15 | 85 16 | 17 | 25 18 | 19 | 31 20 | 21 | 91 22 | 23 | 25 24 | 25 | 35 26 | -------------------------------------------------------------------------------- /dataStructures/stack/StackImplementation.asm: -------------------------------------------------------------------------------- 1 | ##Writer: Minhas Kamal 2 | ##Date: 02-MAY-2014 3 | ##Function: This program is a simple demonstration of stack. 4 | 5 | #####**data**##### 6 | .data 7 | 8 | prompt: .asciiz "\n# What operation do you want to perform?\nPress-\t1.for push \n\t2.for pop \n\t3.for print all \n\t4.for exit \nSelection: " 9 | prompt2: .asciiz "Enter the value: " 10 | conf_pop: .asciiz "\nOperation pop is successful.\n" 11 | conf_push: .asciiz "Operation push is successful.\n" 12 | exit_msg: .asciiz "\n\n\nProgram exits...." 13 | comma_space: .asciiz ", " 14 | 15 | num: .double 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 16 | .double 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 17 | capacity: .word 40 18 | init: .double 0.0 19 | 20 | #####**text**##### 21 | .text 22 | 23 | main: 24 | lw $t1, capacity #load capacity 25 | l.d $f4, init #load the initial value 26 | li $t2, 0 #initial index is 0 27 | 28 | loop: 29 | la $a0, prompt #prompt 30 | li $v0, 4 31 | syscall 32 | 33 | li $v0, 5 #take choice from user 34 | syscall 35 | 36 | la $t0, num #load array 37 | 38 | beq $v0, 1, push 39 | beq $v0, 2, pop 40 | beq $v0, 3, print_all 41 | j exit 42 | 43 | 44 | #------------------------------------------------------------------------------------------------------------------- 45 | #pushes one number on top 46 | push: 47 | li $t3, 0 48 | 49 | beq $t2, $t1, loop #return when capacity is full 50 | 51 | push_loop: #find the address 52 | add $t0, $t0, 8 53 | add $t3, $t3, 1 54 | ble $t3, $t2, push_loop 55 | sub $t0, $t0, 8 #subtract once 56 | 57 | la $a0, prompt2 #prompt for input 58 | li $v0, 4 59 | syscall 60 | 61 | li $v0, 7 #take input 62 | syscall 63 | 64 | s.d $f0, ($t0) #store the value 65 | 66 | add $t2, $t2, 1 #increase index 67 | 68 | la $a0, conf_push #confirmation 69 | li $v0, 4 70 | syscall 71 | 72 | j loop #return 73 | 74 | 75 | #------------------------------------------------------------------------------------------------------------------- 76 | #pops up top number 77 | pop: 78 | li $t3, 0 79 | 80 | beq $t2, $t3, loop #return when nothing to pop 81 | 82 | pop_loop: #find the number 83 | add $t0, $t0, 8 84 | add $t3, $t3, 1 85 | blt $t3, $t2, pop_loop 86 | sub $t0, $t0, 8 #subtract once 87 | 88 | 89 | l.d $f12, ($t0) #print the number 90 | li $v0, 3 91 | syscall 92 | 93 | s.d $f4, ($t0) #initialize 94 | sub $t2, $t2, 1 #reduce index 95 | 96 | la $a0, conf_pop #confirmation 97 | li $v0, 4 98 | syscall 99 | 100 | j loop #return 101 | 102 | #------------------------------------------------------------------------------------------------------------------- 103 | #prints all the values 104 | print_all: 105 | li $t3, 0 106 | print_loop: 107 | l.d $f12, ($t0) #print the number 108 | li $v0, 3 109 | syscall 110 | 111 | la $a0, comma_space #seperate ", " 112 | li $v0, 4 113 | syscall 114 | 115 | add $t0, $t0, 8 116 | add $t3, $t3, 1 #increase the loop counter 117 | blt $t3, $t1, print_loop 118 | j loop 119 | 120 | 121 | #------------------------------------------------------------------------------------------------------------------- 122 | #exit from the program 123 | exit: 124 | la $a0, exit_msg #exit message 125 | li $v0, 4 126 | syscall 127 | 128 | li $v0, 10 129 | syscall 130 | -------------------------------------------------------------------------------- /dataStructures/stack/StackImplementation.c: -------------------------------------------------------------------------------- 1 | /** 2 | *Name: Minhas Kamal 3 | *Program Name: StackImplementation 4 | *Date: 04.May.2013 5 | **/ 6 | 7 | #include 8 | 9 | void stack_size(void); //working functions 10 | void stack_pop(void); 11 | void stack_push(void); 12 | 13 | 14 | char c[5]="*****"; //array 15 | 16 | int main() 17 | { 18 | for( ; ; ) 19 | { 20 | int dis; //decision input 21 | printf("Enter your selection: \npress\n\t1.stack size\n\t2.stack pop\n\t3.stack push\n\t4.quit\n"); //selection 22 | scanf("%d", &dis); 23 | fflush(stdin); 24 | 25 | if(dis==1) stack_size(); //working according to selection & calling function 26 | else if(dis==2) stack_pop(); 27 | else if(dis==3) stack_push(); 28 | else break; //quit 29 | } 30 | return 0; 31 | } 32 | 33 | void stack_size(void) //shows size 34 | { 35 | int i; 36 | for(i=4; i>=0; i--) if(c[i]!='*') break; 37 | printf("\nThe size is: %d\n\n\n", i+1); 38 | } 39 | 40 | void stack_pop(void) //printing char 41 | { 42 | int i; 43 | for(i=4; i>=0; i--) if(c[i]!='*') break; 44 | if(i>=0) 45 | { 46 | printf("\nIt is: %c\n\n\n", c[i]); 47 | c[i]='*'; 48 | } 49 | else printf("\nStack is empty!\n\n\n"); 50 | } 51 | 52 | void stack_push(void) //assigns char 53 | { 54 | int i; 55 | if(c[4]!='*') 56 | { 57 | printf("\nStack is Full.\n\n\n"); 58 | return ; 59 | } 60 | for(i=4; i>=0; i--) if(c[i]!='*') break; 61 | 62 | fflush(stdin); 63 | char ch; 64 | printf("Enter your input: "); 65 | scanf("%c", &ch); 66 | 67 | c[i+1]=ch; 68 | } 69 | 70 | -------------------------------------------------------------------------------- /dataStructures/string/String.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal (BSSE0509, IIT, DU) 3 | * Date: 26-Mar-2013 4 | **/ 5 | 6 | #include 7 | 8 | void strlen(int s1); 9 | void strcat(int s1, int s2); 10 | void strcmp(int s1, int s2); 11 | 12 | char s[20][201]; 13 | 14 | int main() 15 | { 16 | 17 | int x=20; 18 | //scanf("%d", &x); 19 | //printf("Enter the number of strings: "); 20 | 21 | //char s[x][201]; 22 | 23 | int y; 24 | for(y=1; y<=x; y++) 25 | { 26 | printf("Enter string No-%d (not more than 200 chars): ", y); 27 | gets(s[y]); 28 | } 29 | 30 | for( ; ; ) 31 | { 32 | int c; 33 | printf("Enter your choice:\npress\t1 to know the length of string\n\t\t"); 34 | printf("2 to add two strings\n\t\t3 to compare two strings\n\t\t"); 35 | printf("4 to quit.\n\t\t"); 36 | scanf("%d", &c); 37 | 38 | if(c==1) 39 | { 40 | int s1; 41 | printf("Enter string number: "); 42 | scanf("%d", &s1); 43 | strlen(s1); 44 | } 45 | else if(c==2) 46 | { 47 | int s1; 48 | printf("Enter first string number: "); 49 | scanf("%d", &s1); 50 | int s2; 51 | printf("Enter second string number: "); 52 | scanf("%d", &s2); 53 | strcat(s1, s2); 54 | } 55 | else if(c==3) 56 | { 57 | int s1; 58 | printf("Enter first string number: "); 59 | scanf("%d", &s1); 60 | int s2; 61 | printf("Enter second string number: "); 62 | scanf("%d", &s2); 63 | strcmp(s1, s2); 64 | } 65 | else if(c==4) break; 66 | else if(c<1 || c>4) {printf("Wrong input!"); continue ;} 67 | 68 | 69 | } 70 | 71 | return 0; 72 | } 73 | 74 | 75 | void strlen(int s1) 76 | { 77 | int c1; 78 | for(c1=0; s[s1][c1]!='0'; c1++); 79 | 80 | printf("Number of chars: %d", c1); 81 | 82 | return ; 83 | } 84 | 85 | 86 | void strcat(int s1, int s2) 87 | { 88 | int c1=0; 89 | char ns[402]; 90 | 91 | int y=0; 92 | for( ; s[s1][c1]!='0'; ) 93 | { 94 | ns[y] = s[s1][c1]; 95 | c1++; 96 | y++; 97 | } 98 | 99 | int z=y, x=0; 100 | for( ; s[s2][x]; ) 101 | { 102 | ns[z] = s[s2][x]; 103 | x++; 104 | z++; 105 | } 106 | ns[z]=0; 107 | 108 | int v=0; 109 | for( ; ns[v]; v++)printf("%c", ns[v]); 110 | 111 | return ; 112 | } 113 | 114 | 115 | void strcmp(int s1, int s2) 116 | { 117 | int f=1, c1=0, c2=0; 118 | for( ; s[s1][c1]!=0 && s[s2][c2]!=0; ) 119 | { 120 | if(s[s1][c1] != s[s2][c2]) f=0; 121 | 122 | c1++; 123 | c2++; 124 | } 125 | if(s[s1][c1]==0 && s[s2][c2]!=0) f=-1; 126 | else if(s[s2][c2]==0 && s[s1][c1]!=0) f=2; 127 | 128 | if(f==1) printf("String matched."); 129 | else if(f==2) printf("The first one is bigger."); 130 | else if(f==-1) printf("The second one is bigger."); 131 | else if(f==0) printf("Size is same but doesn't match."); 132 | 133 | return ; 134 | } 135 | -------------------------------------------------------------------------------- /dataStructures/string/StringLongestCommonSubSecuence.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal 3 | * Date: 23-Mar-2014 4 | **/ 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | 12 | void LCSFinder(char *firstString, char *secondString); 13 | int stringLength(char *string); 14 | void printLcs(char **direction, char *firstString, int i, int j); 15 | 16 | 17 | int main(){ 18 | int length = 100; 19 | char firstString[length], 20 | secondString[length]; 21 | 22 | cout << "Enter two strings: "; 23 | cin >> firstString >> secondString; 24 | 25 | cout << "Longest common sub-sequence is: "; 26 | LCSFinder(firstString, secondString); 27 | cout << "\n"; 28 | 29 | 30 | return 0; 31 | } 32 | 33 | 34 | void LCSFinder(char *firstString, char *secondString){ 35 | int firstStringLength = stringLength(firstString), 36 | secondStringLength = stringLength(secondString); 37 | //cout << firstStringLength << " " << secondStringLength; 38 | 39 | int index[firstStringLength+1][secondStringLength+1]; 40 | 41 | char **direction; 42 | direction = new char* [firstStringLength]; 43 | for(int i=0; i=index[i][j-1]){ 60 | index[i][j]=index[i-1][j]; 61 | direction[i-1][j-1]='|'; //up 62 | } 63 | else{ 64 | index[i][j]=index[i][j-1]; 65 | direction[i-1][j-1]='-'; //side 66 | } 67 | } 68 | } 69 | 70 | cout << "\nMatrix: \n"; 71 | for(int i=0; i 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | #define X 30 14 | 15 | int main(){ 16 | int array[X+1]; 17 | array[0]=0; 18 | for(int i=1; i=array[j]){ 36 | if(position[j]>position[i]){ 37 | position[i]=position[j]; 38 | prevIndex[i]=j; 39 | } 40 | } 41 | } 42 | position[i]++; 43 | } 44 | 45 | ///traverse ///test 46 | /*for(int i=0; ilength){ 55 | length=position[i]; 56 | index=i; 57 | } 58 | } 59 | 60 | ///sub sequence 61 | stack subSequence; 62 | while(prevIndex[index]!=0){ 63 | subSequence.push(array[index]); 64 | index = prevIndex[index]; 65 | } 66 | subSequence.push(array[index]); 67 | 68 | while(!subSequence.empty()){ 69 | cout << subSequence.top() << endl; 70 | subSequence.pop(); 71 | } 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /dataStructures/tree/BinomialTree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Programmer: Minhas Kamal (BSSE0509,IIT,DU) 3 | * Date: 30-Mar-2014 4 | **/ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | #define X 3 13 | 14 | struct node{ 15 | int value; 16 | int depth; 17 | node *previous; 18 | node *next[X]; 19 | } nullNode; 20 | 21 | 22 | 23 | int main(){ 24 | ///input array 25 | int arrayLength=1; 26 | for(int i=0; idepth==binomialTree[newNode].depth) 64 | similarityFound=true; 65 | } 66 | 67 | if(similarityFound){ 68 | if(binomialTree[j].value < binomialTree[newNode].value){ 69 | binomialTree[j].next[binomialTree[j].depth]=&binomialTree[newNode]; 70 | binomialTree[newNode].previous=&binomialTree[j]; 71 | newNode=j; 72 | binomialTree[j].depth++; 73 | } 74 | else{ 75 | binomialTree[newNode].next[binomialTree[newNode].depth]=&binomialTree[j]; 76 | binomialTree[j].previous=&binomialTree[newNode]; 77 | newNode=newNode; 78 | binomialTree[newNode].depth++; 79 | } 80 | 81 | nullNode.depth--; 82 | nullNode.next[nullNode.depth]=&binomialTree[newNode]; 83 | 84 | }else{ 85 | break; 86 | } 87 | 88 | } 89 | } 90 | 91 | ///traversing 92 | for(int i=0; i 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main(int argc, char *argv[]){ 13 | //**taking the file name 14 | string fileName=""; 15 | if(argc>1){ 16 | fileName=argv[1]; 17 | }else{ 18 | cout << "Enter the file name: "; 19 | cin >> fileName; 20 | //fileName="ASInput.txt";//test 21 | } 22 | 23 | 24 | //**opening the file 25 | ifstream input; 26 | input.open(fileName.c_str()); 27 | if(!input.is_open()){ //when the file name is wrong 28 | printf("Sorry the file is not found!"); 29 | input.close(); 30 | return 1; 31 | } 32 | 33 | 34 | //**reading file 35 | int n; 36 | input >> n; 37 | int time[n][3]; ///colour, start position, stop position 38 | 39 | int i=0; 40 | for(; i> time[i][1] >> time[i][2]; 43 | } 44 | input.close(); 45 | 46 | 47 | /*////test 48 | int t; 49 | cout << endl; 50 | for(t=0; t touples = new LinkedList(); 26 | 27 | Scanner scanner = new Scanner(System.in); 28 | System.out.println("Enter your touples(0 for exit): "); 29 | 30 | String[] tempStrings; 31 | tempStrings = scanner.nextLine().split(" "); 32 | while(!tempStrings[0].equals("0")){ 33 | touples.add(new Touple(tempStrings)); 34 | tempStrings = scanner.nextLine().split(" "); 35 | } 36 | 37 | return touples.toArray(new Touple[0]); 38 | } 39 | 40 | private Touple[] takeInput2(){ 41 | LinkedList touples = new LinkedList(); 42 | 43 | int[] i = new int[3]; 44 | 45 | i[0]=2; i[1]=3; i[2]=9; touples.add(new Touple(i.clone())); 46 | i[0]=4; i[1]=9; i[2]=4; touples.add(new Touple(i.clone())); 47 | i[0]=3; i[1]=5; i[2]=0; touples.add(new Touple(i.clone())); 48 | i[0]=6; i[1]=8; i[2]=2; touples.add(new Touple(i.clone())); 49 | i[0]=8; i[1]=1; i[2]=1; touples.add(new Touple(i.clone())); 50 | i[0]=1; i[1]=4; i[2]=5; touples.add(new Touple(i.clone())); 51 | i[0]=6; i[1]=2; i[2]=8; touples.add(new Touple(i.clone())); 52 | i[0]=7; i[1]=3; i[2]=2; touples.add(new Touple(i.clone())); 53 | 54 | return touples.toArray(new Touple[0]); 55 | } 56 | 57 | private void traverse(Touple[] touples) { 58 | String str = "[" + touples[0].dupm(); 59 | for(int i=1; i touples[j].values[elementNumber]){ 71 | Touple temp = touples[i]; 72 | touples[i] = touples[j]; 73 | touples[j] = temp; 74 | } 75 | } 76 | } 77 | } 78 | 79 | private void constructBalanceTree(Touple[] touples, int startRange, int finishRange, int step) { 80 | 81 | partialSort(touples, startRange, finishRange, step%touples[0].values.length); 82 | 83 | int divider = (startRange+finishRange)/2; 84 | System.out.println(touples[divider].values[step%touples[0].values.length]); 85 | traverse(touples); 86 | 87 | if(finishRange-startRange>2){ 88 | step++; 89 | constructBalanceTree(touples, startRange, divider, step); 90 | constructBalanceTree(touples, divider, finishRange, step); 91 | } 92 | 93 | return ; 94 | } 95 | 96 | public static void main(String[] args) { 97 | BalanceTree balanceTree = new BalanceTree(); 98 | balanceTree.CreateBalanceTree(); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /dataStructures/tree/balanceTree/Touple.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Developer: Minhas Kamal(BSSE0509, IIT, DU) 3 | * Date: 09-Feb-2015 4 | **/ 5 | 6 | package balanceTree; 7 | 8 | public class Touple { 9 | public int[] values; 10 | 11 | public Touple() { 12 | values = new int[3]; 13 | for(int i=0; i<3; i++){ 14 | values[i] = 0; 15 | } 16 | } 17 | 18 | public Touple(int[] values) { 19 | this.values = values; 20 | } 21 | 22 | public Touple(String[] values) { 23 | this.values = new int[values.length]; 24 | 25 | for(int i=0; i> tree = new LinkedList<>(); 14 | 15 | public static void main(String[] args) { 16 | //data = convertStringToInt(takeInput()); 17 | 18 | data = takeInput2(); 19 | traverse(data); 20 | System.out.println("\n##################\n"); 21 | 22 | int dimention = 0; 23 | int level = 0; 24 | constructTree(data, 0, data.length, dimention, level); 25 | 26 | System.out.println("\n##################\n"); 27 | traverse(data); 28 | 29 | printTree(tree); 30 | } 31 | 32 | private static String[][] takeInput(){ 33 | LinkedList inputData = new LinkedList<>(); 34 | 35 | @SuppressWarnings("resource") 36 | Scanner scanner = new Scanner(System.in); 37 | System.out.println("Enter your input (0 for exit): "); 38 | String[] inputStr = scanner.nextLine().split(" "); 39 | while(!inputStr[0].equals("0")){ 40 | inputData.add(inputStr); 41 | inputStr = scanner.nextLine().split(" "); 42 | } 43 | 44 | String[][] stringData = new String[inputData.size()][]; 45 | stringData = inputData.toArray(stringData); 46 | 47 | return stringData; 48 | } 49 | 50 | private static int[][] convertStringToInt(String[][] strdata){ 51 | int firstDLen = strdata.length; 52 | int secondDLen = strdata[0].length; 53 | 54 | int[][] data = new int[firstDLen][secondDLen]; 55 | for(int i=0; i data[j][dimention]){ 93 | int[] temp = data[i]; 94 | data[i] = data[j]; 95 | data[j] = temp; 96 | } 97 | } 98 | } 99 | 100 | //if there is more than one member 101 | if(to-from > 1){ 102 | traverse(data);//output 103 | if(tree.size() ints = new LinkedList(); 105 | tree.add(ints); 106 | } 107 | tree.get(level).add(data[(from+to)/2][dimention]); 108 | 109 | level++; 110 | dimention++; 111 | if(dimention>=data[0].length){ 112 | dimention=0; 113 | } 114 | 115 | //recursive call 116 | constructTree(data, from, (from+to)/2, dimention, level); 117 | constructTree(data, (from+to)/2, to, dimention, level); 118 | } 119 | 120 | return; 121 | } 122 | 123 | private static void printTree(LinkedList> tree){ 124 | for(LinkedList elem: tree){ 125 | for(int node: elem){ 126 | System.out.println(node + "\t"); 127 | } 128 | System.out.println(); 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /graph/breathFirstSearch/BreadthFirstSearch.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Developer: Minhas Kamal (BSSE-0509, IIT, DU) 3 | * Date: 12.Nov.2013 4 | **/ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | void printPath(int* colour, int* distance, int* previous, int startVertex, int queryVertex); 13 | 14 | int main()//(int argc, char* argv[]) 15 | { 16 | ifstream graph; //input file stream 17 | graph.open("BreadthFirstSearch_Input2.txt"); 18 | //graph.open(argv[1]); 19 | 20 | if(!graph.is_open()){ //when file is not found 21 | cout << "File name is wrong!\n"; 22 | return 0; 23 | } 24 | 25 | int vertexNumber, **matrix; 26 | graph >> vertexNumber; 27 | matrix = new int* [vertexNumber]; 28 | for(int i=0; i> matrix[i][j]; 34 | 35 | 36 | 37 | /*for(int i=0; i> startVertex; 58 | startVertex = startVertex-1; 59 | 60 | ///Start of BFS 61 | queue bFSQueue; 62 | bFSQueue.push(startVertex); 63 | 64 | colour[startVertex]=2; //colour is set to grey 65 | distance[startVertex]=0; //distance is zero 66 | previous[startVertex]=-2; //there is no previous 67 | 68 | while(!bFSQueue.empty()){ //finding adjacent vertexes and colouring them 69 | int i=bFSQueue.front(); 70 | bFSQueue.pop(); 71 | colour[i]=3; 72 | 73 | for(int j=0; j> queryVertex; 91 | queryVertex = queryVertex-1; 92 | 93 | cout << "\n\nThe Path is: \n"; 94 | printPath(colour, distance, previous, startVertex, queryVertex); 95 | 96 | 97 | for(int i=0; i 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main()//(int argc, char* argv[]) 13 | { 14 | ifstream graph; //input file stream 15 | graph.open("DepthFirstSearch_Input.txt"); 16 | //graph.open(argv[1]); 17 | 18 | if(!graph.is_open()){ //when file is not found 19 | cout << "File name is wrong!\n"; 20 | return 0; 21 | } 22 | 23 | int vertexNumber, **matrix; 24 | graph >> vertexNumber; 25 | matrix = new int* [vertexNumber]; 26 | for(int i=0; i> matrix[i][j]; 32 | 33 | 34 | 35 | /*/for(int i=0; i> startVertex; 52 | startVertex = startVertex-1; 53 | 54 | ///start of DFS 55 | stack dFSStack; 56 | dFSStack.push(startVertex); 57 | 58 | colour[startVertex]=2; //colour is set to grey 59 | 60 | cout << "Reachable vertexes are: \n"; 61 | while(!dFSStack.empty()){ 62 | int i=dFSStack.top(); 63 | dFSStack.pop(); 64 | 65 | for(int j=0; j 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | 14 | int FordFulkerson(int **matrix, int vertexNumber, int s, int t); 15 | bool BFS(int **residualGraph, int vertexNumber, int s, int t, int parent[]); 16 | 17 | 18 | int main(){ 19 | ifstream graph; //input file stream 20 | graph.open("MaxFlow_Input.txt"); 21 | 22 | if(!graph.is_open()){ //when file is not found 23 | cout << "File name is wrong!\n"; 24 | return 0; 25 | } 26 | 27 | int vertexNumber, **matrix; 28 | graph >> vertexNumber; 29 | matrix = new int* [vertexNumber]; 30 | for(int i=0; i> matrix[i][j]; 36 | 37 | graph.close(); //close the file graph 38 | 39 | /*for(int i=0; i residualGraph[parent[v]][v]){ 72 | pathFlow=residualGraph[parent[v]][v]; 73 | } 74 | } 75 | 76 | 77 | for(int v=t; v!=s; v=parent[v]){ 78 | residualGraph[parent[v]][v] = residualGraph[parent[v]][v] - pathFlow; 79 | residualGraph[v][parent[v]] = residualGraph[parent[v]][v] + pathFlow; 80 | } 81 | 82 | maxFlow = maxFlow+pathFlow; 83 | } 84 | 85 | return maxFlow; 86 | } 87 | 88 | 89 | //classic BFS 90 | bool BFS(int **residualGraph, int vertexNumber, int s, int t, int parent[]){ 91 | 92 | bool visited[vertexNumber]; 93 | for(int i=0; i vertexQueue; 98 | 99 | vertexQueue.push(s); 100 | parent[s]=-1; 101 | visited[s]=true; 102 | 103 | while(!vertexQueue.empty()){ 104 | int v=vertexQueue.front(); 105 | vertexQueue.pop(); 106 | 107 | for(int i=0; i0){ 109 | vertexQueue.push(i); 110 | parent[i]=v; 111 | visited[i]=true; 112 | } 113 | } 114 | } 115 | 116 | return visited[t]; 117 | } 118 | 119 | -------------------------------------------------------------------------------- /graph/maxFlow/MaxFlow_Input.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MinhasKamal/AlgorithmImplementations/29e45b8b19aaea9a1946fbdfa2da585c5f09b118/graph/maxFlow/MaxFlow_Input.png -------------------------------------------------------------------------------- /graph/maxFlow/MaxFlow_Input.txt: -------------------------------------------------------------------------------- 1 | 6 2 | 0 12 5 0 8 0 3 | 0 0 2 0 0 7 4 | 0 0 0 14 0 0 5 | 0 0 0 0 0 15 6 | 0 0 0 3 0 0 7 | 0 0 0 0 0 0 -------------------------------------------------------------------------------- /graph/shortestPathAlgorithm/ShortestPathAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Developer: Minhas Kamal (BSSE-0509, IIT, DU) 3 | * Date: 13.Nov.2013 4 | **/ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | void printPath(int* distance, int* previous, int startVertex, int queryVertex); 13 | 14 | int main(){ 15 | ifstream graph; //input file stream 16 | graph.open("ShortestPathAlgorithm_Input.txt"); 17 | //graph.open(argv[1]); 18 | 19 | if(!graph.is_open()){ //when file is not found 20 | cout << "File name is wrong!\n"; 21 | return 0; 22 | } 23 | 24 | int vertexNumber, **matrix; 25 | graph >> vertexNumber; 26 | matrix = new int* [vertexNumber]; 27 | for(int i=0; i> matrix[i][j]; 33 | 34 | 35 | 36 | /*/for(int i=0; i> startVertex; 56 | startVertex = startVertex-1; 57 | 58 | ///Start of SPA 59 | queue SPAQueue; 60 | SPAQueue.push(startVertex); 61 | 62 | distance[startVertex]=0; //distance is zero 63 | previous[startVertex]=-2; //there is no previous 64 | 65 | while(!SPAQueue.empty()){ //finding adjacent vertexes and colouring them 66 | int i=SPAQueue.front(); 67 | SPAQueue.pop(); 68 | 69 | for(int j=0; jdistance[i]+matrix[i][j])){ 71 | distance[j]=distance[i]+matrix[i][j]; 72 | previous[j] =i; 73 | SPAQueue.push(j); 74 | } 75 | } 76 | 77 | /*/cout << endl; 78 | for(int i=0; i> queryVertex; 85 | queryVertex = queryVertex-1; 86 | 87 | cout << "\n\nThe distance is: " << distance[queryVertex]; 88 | 89 | cout << "\nThe Path is: \n"; 90 | printPath(distance, previous, startVertex, queryVertex); 91 | 92 | 93 | for(int i=0; i 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main()//(int argc, char* argv[]) 13 | { 14 | ifstream graph; //input file stream 15 | graph.open("TopologicalSort_Input.txt"); 16 | //graph.open(argv[1]); 17 | 18 | if(!graph.is_open()){ //when file is not found 19 | cout << "File name is wrong!\n"; 20 | return 0; 21 | } 22 | 23 | int vertexNumber, **matrix; 24 | graph >> vertexNumber; 25 | matrix = new int* [vertexNumber]; 26 | for(int i=0; i> matrix[i][j]; 32 | 33 | 34 | 35 | /*for(int i=0; i sortedList; 51 | for(int x=0; x dFSStack; 55 | dFSStack.push(startVertex); 56 | sortedList.push(startVertex); 57 | 58 | colour[startVertex]=2; //colour is set to grey 59 | 60 | while(!dFSStack.empty()){ 61 | int i=dFSStack.top(); 62 | dFSStack.pop(); 63 | 64 | for(int j=0; j=0; i--) 94 | cout << "\t\t" << result[i]+1 << endl; 95 | 96 | 97 | for(int i=0; i 7 | #include 8 | 9 | 10 | int main(){ 11 | int n=201; 12 | 13 | double x[n], fx[n], fx2[n]; 14 | 15 | double a=0.0, b=2*M_PI; 16 | double h=(b-a)/n; 17 | 18 | 19 | int i; 20 | x[0]=a; 21 | fx[0]=sin(x[0]); //calculation of function 22 | for(i=1; i 7 | #include 8 | 9 | 10 | #define f(x) sin(x) 11 | 12 | 13 | int main(){ 14 | int n=100; 15 | double a=0.0, b=M_PI; 16 | double h=(b-a)/n; 17 | 18 | double I=0; 19 | int i; 20 | 21 | I = I + 0.5*f(a); 22 | for(i=1; i 7 | #include 8 | 9 | 10 | #define f(x) sin(x) 11 | 12 | 13 | int main(){ 14 | int n=100; 15 | double a=0.0, b=M_PI; 16 | double h=(b-a)/n; 17 | 18 | double I=0; 19 | int i; 20 | 21 | I = I + f(a); 22 | for(i=1; i 7 | #include 8 | 9 | 10 | #define f1(x) ((x)-5) * ((x)-5) 11 | #define f2(x) 0 12 | #define f(x) (f1(x))-(f2(x)) 13 | 14 | int main(){ 15 | int n=100; 16 | double a=-5, b=5; 17 | double h=(b-a)/n; 18 | 19 | double I=0; 20 | int i; 21 | 22 | I = I + 0.5*f(a); 23 | for(i=1; i 7 | 8 | 9 | void JacobisMethod(int n, double x[n], double b[n], double a[n][n]); 10 | void GaussSeidalMethod(int n, double x[n], double b[n], double a[n][n]); 11 | void print(int n, double x[n]); 12 | 13 | 14 | int main(){ 15 | int n=3; //number of variables 16 | 17 | double x[n]; //variables 18 | 19 | double b[n], //constants 20 | a[n][n]; //arguments 21 | 22 | //assign values 23 | a[0][0]=8; a[0][1]=2; a[0][2]=-2; b[0]=8; 24 | a[1][0]=1; a[1][1]=-8; a[1][2]=3; b[1]=-4; 25 | a[2][0]=2; a[2][1]=1; a[2][2]=9; b[2]=12; 26 | 27 | int i; 28 | 29 | for(i=0; i -0.000001 && (Nx[i]-x[i])/x[i] < 0.000001 )){ 64 | rootFound=0; 65 | break; 66 | } 67 | } 68 | 69 | for(i=0; i -0.000001 && (Nx[i]-x[i])/x[i] < 0.000001 )){ 100 | rootFound=0; 101 | break; 102 | } 103 | } 104 | 105 | for(i=0; i 7 | #include 8 | 9 | 10 | /// Here define different functions to work with 11 | //1st function set 12 | #define f(x) ( ((x)*(x)*(x)) - (x) - 2 ) 13 | #define f2(x) ( (3*(x)*(x)) - 1 ) 14 | #define g(x) ( cbrt( (x) + 2 ) ) 15 | //2nd function set 16 | //#define f(x) ( ((x)*(x)*(x)) - (4*(x)) - 8.95 ) 17 | //#define f2(x) ( (3*(x)*(x)) - 4 ) 18 | //#define g(x) ( cbrt( (4*(x)) + 8.95 ) ) 19 | 20 | 21 | //functions using different methods in solving the same problem 22 | double BisectionMethod(); 23 | double FalsePosition(); 24 | double NewtonRaphson(); 25 | double FixedPoint(); 26 | double Secant(); 27 | 28 | 29 | int main(){ 30 | double root; 31 | 32 | root = BisectionMethod(); 33 | printf("Using Bisection Method the root is: %lf \n\n", root); 34 | 35 | root = FalsePosition(); 36 | printf("Using False Position Method the root is: %lf \n\n", root); 37 | 38 | root = NewtonRaphson(); 39 | printf("Using Newton-Raphson Method the root is: %lf \n\n", root); 40 | 41 | root = FixedPoint(); 42 | printf("Using Fixed Point Method the root is: %lf \n\n", root); 43 | 44 | root = Secant(); 45 | printf("Using Secant Method the root is: %lf \n\n", root); 46 | 47 | return 0; 48 | } 49 | 50 | /** 51 | * takes two initial values and shortens the distance by both side 52 | **/ 53 | double BisectionMethod(){ 54 | double root=0; 55 | 56 | double a=1, b=2; 57 | double c=0; 58 | 59 | int loopCounter=0; 60 | if(f(a)*f(b) < 0){ 61 | while(1){ 62 | loopCounter++; 63 | c=(a+b)/2; 64 | 65 | if(f(c)<0.00001 && f(c)>-0.00001){ 66 | root=c; 67 | break; 68 | } 69 | 70 | if((f(a))*(f(c)) < 0){ 71 | b=c; 72 | }else{ 73 | a=c; 74 | } 75 | 76 | } 77 | } 78 | printf("It took %d loops.\n", loopCounter); 79 | 80 | return root; 81 | } 82 | 83 | /** 84 | * takes two initial values and shortens the distance by single side 85 | **/ 86 | double FalsePosition(){ 87 | double root=0; 88 | 89 | double a=1, b=2; 90 | double c=0; 91 | 92 | int loopCounter=0; 93 | if(f(a)*f(b) < 0){ 94 | while(1){ 95 | loopCounter++; 96 | 97 | c=(a*f(b) - b*f(a)) / (f(b) - f(a)); 98 | 99 | /*/printf("%lf\t %lf \n", c, f(c));/**////test 100 | if(f(c)<0.00001 && f(c)>-0.00001){ 101 | root=c; 102 | break; 103 | } 104 | 105 | if((f(a))*(f(c)) < 0){ 106 | b=c; 107 | }else{ 108 | a=c; 109 | } 110 | } 111 | } 112 | printf("It took %d loops.\n", loopCounter); 113 | 114 | return root; 115 | } 116 | 117 | /** 118 | * uses one initial value and gradually takes that value near to the real one 119 | **/ 120 | double NewtonRaphson(){ 121 | double root=0; 122 | 123 | double x1=1; 124 | double x2=0; 125 | 126 | int loopCounter=0; 127 | while(1){ 128 | loopCounter++; 129 | 130 | x2 = x1 - (f(x1)/f2(x1)); 131 | /*/printf("%lf \t %lf \n", x2, f(x2));/**////test 132 | 133 | if(f(x2)<0.00001 && f(x2)>-0.00001){ 134 | root=x2; 135 | break; 136 | } 137 | 138 | x1=x2; 139 | } 140 | printf("It took %d loops.\n", loopCounter); 141 | 142 | return root; 143 | } 144 | 145 | /** 146 | * uses one initial value and gradually takes that value near to the real one 147 | **/ 148 | double FixedPoint(){ 149 | double root=0; 150 | double x=1; 151 | 152 | int loopCounter=0; 153 | while(1){ 154 | loopCounter++; 155 | 156 | if( (x-g(x)) <0.00001 && (x-g(x)) >-0.00001){ 157 | root = x; 158 | break; 159 | } 160 | 161 | /*/printf("%lf \t %lf \n", g(x), x-(g(x)));/**////test 162 | 163 | x=g(x); 164 | } 165 | printf("It took %d loops.\n", loopCounter); 166 | 167 | return root; 168 | } 169 | 170 | /** 171 | * uses two initial values & both value approaches to the root 172 | **/ 173 | double Secant(){ 174 | double root=0; 175 | 176 | double x0=1; 177 | double x1=2; 178 | double x2=0; 179 | 180 | int loopCounter=0; 181 | while(1){ 182 | loopCounter++; 183 | 184 | /*/printf("%lf \t %lf \t %lf \n", x0, x1, f(x1));/**////test 185 | 186 | if(f(x1)<0.00001 && f(x1)>-0.00001){ 187 | root=x1; 188 | break; 189 | } 190 | 191 | x2 = ((x0*f(x1))-(x1*f(x0))) / (f(x1)-f(x0)); 192 | 193 | x0=x1; 194 | x1=x2; 195 | } 196 | printf("It took %d loops.\n", loopCounter); 197 | 198 | return root; 199 | } 200 | 201 | -------------------------------------------------------------------------------- /others/cigaretteSmokersProblem/Agent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal (BSSE-0509, IIT, DU) 3 | * Date: 04-Dec-2014 4 | **/ 5 | 6 | package cigaretteSmokersProblem; 7 | 8 | import java.util.LinkedList; 9 | import java.util.Queue; 10 | import java.util.Random; 11 | 12 | public class Agent { 13 | private Smoker[] smokers; 14 | 15 | public Agent(Smoker[] smokers) { 16 | this.smokers = smokers; 17 | } 18 | 19 | public Queue start(int time){ 20 | Queue symbols = new LinkedList(); 21 | 22 | int i=0; 23 | while(i symbols = agent.start(800); 21 | 22 | new Graph(symbols).setVisible(true); 23 | } 24 | 25 | 26 | public static void main(String[] args) { 27 | new CigaretteSmokersProblemMain(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /others/cigaretteSmokersProblem/Graph.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal (BSSE-0509, IIT, DU) 3 | * Date: 04-Dec-2014 4 | **/ 5 | 6 | package cigaretteSmokersProblem; 7 | 8 | import java.awt.Color; 9 | import java.util.*; 10 | import javax.swing.*; 11 | 12 | @SuppressWarnings("serial") 13 | public class Graph extends JFrame{ 14 | //** 15 | // Variable Declaration #*******D*******# 16 | //** 17 | private JPanel jPanelMain; 18 | private JPanel[] jPanelGraph; 19 | 20 | //other variables 21 | private final int LENGTH = 640; 22 | 23 | private int parts; 24 | // End of Variable Declaration #_______D_______# 25 | 26 | /***##Constructor##***/ 27 | public Graph(Queue symbols) { 28 | parts = symbols.size(); 29 | 30 | initialComponent(symbols); 31 | } 32 | 33 | 34 | /** 35 | * Method for Initializing all the GUI variables, placing them all to specific space on the frame and adding action 36 | * listener to them. Also specifies criteria of the main frame. 37 | */ 38 | private void initialComponent(Queue symbols) { 39 | //** 40 | // Initialization #*******I*******# 41 | //** 42 | jPanelMain = new JPanel(); 43 | jPanelGraph = new JPanel[parts]; 44 | //other variables 45 | Queue uniqueSymbols = new LinkedList(); 46 | // End of Initialization #_______I_______# 47 | 48 | //** 49 | // Setting Bounds and Attributes of the Elements #*******S*******# 50 | //** 51 | jPanelMain.setBounds(0, 0, LENGTH+40, 250); 52 | jPanelMain.setBackground(new Color(-1)); 53 | jPanelMain.setLayout(null); 54 | 55 | int partLength = LENGTH/parts; 56 | for(int i=0; i symbol = new LinkedList(); 109 | symbol.add('a');symbol.add('a');symbol.add('b');symbol.add('c'); 110 | symbol.add('a');symbol.add('c');symbol.add('c');symbol.add('d'); 111 | symbol.add('a');symbol.add('b');symbol.add('c');symbol.add('e'); 112 | symbol.add('f');symbol.add('f');symbol.add('f');symbol.add('c'); 113 | symbol.add('a');symbol.add('g');symbol.add('g');symbol.add('d'); 114 | symbol.add('h');symbol.add('h');symbol.add('h');symbol.add('e'); 115 | symbol.add('a');symbol.add('a');symbol.add('c');symbol.add('c'); 116 | symbol.add('i');symbol.add('j');symbol.add('k');symbol.add('i'); 117 | symbol.add('j');symbol.add('j');symbol.add('j');symbol.add('k'); 118 | 119 | Graph gui = new Graph(symbol); 120 | gui.setVisible(true); 121 | } 122 | 123 | //** 124 | // Auxiliary Methods #********AM*******# 125 | //** 126 | 127 | private int getPosition(char symbol, Queue uniqueSymbols){ 128 | int position = -1; 129 | 130 | int p = 0; 131 | for(Character uniqueSymbol: uniqueSymbols){ 132 | if(symbol==uniqueSymbol){ 133 | position=p; 134 | break; 135 | } 136 | p++; 137 | } 138 | 139 | return position; 140 | } 141 | 142 | private Color getColor(int i){ 143 | i *= 150; 144 | 145 | int r = i%256; 146 | int g = i%200+i%56; 147 | int b = i%100+i%156; 148 | 149 | Color color = new Color(r, g, b); 150 | return color; 151 | } 152 | // End of Auxiliary Methods #________AM_______# 153 | } 154 | -------------------------------------------------------------------------------- /others/cigaretteSmokersProblem/Smoker.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal (BSSE-0509, IIT, DU) 3 | * Date: 04-Dec-2014 4 | **/ 5 | 6 | package cigaretteSmokersProblem; 7 | 8 | public class Smoker { 9 | public char ingredient; 10 | public int smokingTime; 11 | 12 | public Smoker(char ingredient, int smokingTime) { 13 | this.ingredient = ingredient; 14 | this.smokingTime = smokingTime; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /others/geneticAlgorithm/Board.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Developer: Minhas Kamal (BSSE-0509, IIT, DU) 3 | * Date: 01.Sep.2015 4 | * Comment: This is a chess board made only for queens. 5 | **/ 6 | 7 | package nQueenProblem; 8 | 9 | import java.util.Random; 10 | 11 | public class Board { 12 | private int boardSize; 13 | private int[][] boxes; 14 | int[] geneticSequence; 15 | 16 | 17 | public Board(int boardSize) { 18 | this.boardSize = boardSize; 19 | boxes = new int[boardSize][boardSize]; 20 | geneticSequence = new int[boardSize]; 21 | 22 | initializeBoard(); 23 | } 24 | 25 | public void initializeBoard(){ 26 | for(int i=0; i || 45 | noOfAttacks += countAttackVertical(queenColumnNo); 46 | //right-diagonal search -> \\ 47 | noOfAttacks += countAttackRightDiagonal(queenColumnNo, row); 48 | //left-diagonal search -> // 49 | noOfAttacks += countAttackLeftDiagonal(queenColumnNo, row); 50 | } 51 | 52 | return noOfAttacks/2; 53 | } 54 | 55 | private int countAttackVertical(int queenColumnNo){ 56 | int noOfAttacks=0; 57 | 58 | for(int k=0; k=boardSize){ //not found 94 | startRow += (startColumn-boardSize+1); 95 | startColumn=boardSize-1; 96 | } 97 | for(int m=startRow, n=startColumn; m=0; m++, n--){ 98 | if(boxes[m][n]==1){ 99 | noOfAttacks++; 100 | } 101 | } 102 | 103 | noOfAttacks--; 104 | 105 | return noOfAttacks; 106 | } 107 | 108 | public int findQueen(int row){ 109 | int queenColumnNo=-1; 110 | for(int j=0; j amount){ 58 | strongAmount = amount; 59 | strong = i; 60 | } 61 | } 62 | 63 | if(board[strong].countAttack()==0){ 64 | System.out.println("\n\n\n## SOLVED!!\n***************\n"+board[strong].toString()+"***************"); 65 | break; 66 | } 67 | 68 | board[weak] = board[strong].getCopy(); 69 | 70 | for(int i=0; i=5000){ 82 | System.out.println("\n\n\n## UNSOLVED!!!!!!!"); 83 | } 84 | } 85 | 86 | 87 | public static void main(String[] args) { 88 | Player player = new Player(); 89 | player.startEvolution(); 90 | 91 | // for(int i=0; i<4; i++){ 92 | // System.out.print(player.board[i].toString()); 93 | // System.out.println(player.board[i].countAttack()+"\n\n\n"); 94 | // } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /others/huffmanAlgorithm/Extractor.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Name: Minhas Kamal (BSSE-0509, IIT, DU) 3 | Description: This program reads a zip file which ends with '#' & extracts the file. 4 | Date: 21-Oct-2013 5 | **/ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | ifstream input; //file reader 14 | struct node{ 15 | int value; //value of the node 16 | char info; //character that it contains 17 | bool bit; 18 | node *r, //root 19 | *t1, //left sub tree 20 | *t2; //right sub tree 21 | }; 22 | 23 | 24 | void treeBuilder(int* num, node* tree, int x, int S, int E); //creates the linked tree 25 | void fileExtractor(node* tree, int x); //writes the extracted file 26 | 27 | int main(){ 28 | input.open("huffmanAlgorithm_Input_Zip.txt"); //opening the file 29 | 30 | if(!input.is_open()){ //when the file name is wrong 31 | printf("Sorry the file is not found!"); 32 | input.close(); 33 | return 0; 34 | } 35 | 36 | int S, //the starting char in the ascii table 37 | E; //the ending char in the ascii table 38 | 39 | input >> S >> E ; 40 | //cout << S << " " << E <> num[i]; 45 | //cout << num [i] << " "; ///test 46 | } 47 | //cout << endl; ///test 48 | 49 | int x=(E-S)*2-1; 50 | node tree[x]; 51 | 52 | treeBuilder(num, tree, x, S, E); 53 | 54 | fileExtractor(tree, x); 55 | 56 | input.close(); 57 | 58 | cout << "File Extracted.\n\n"; 59 | 60 | return 0; 61 | } 62 | 63 | 64 | void treeBuilder(int* num, node* tree, int x, int S, int E){ 65 | for(int i=0; i=0){ 87 | lt=i; 88 | break; 89 | } 90 | 91 | for(int i=0; i=0) 93 | if(tree[i].value < tree[lt].value) lt=i; 94 | } 95 | tree[lt].r = &tree[position]; //setting the root of the left sub trees 96 | 97 | 98 | for(int i=0; i=0){ 100 | rt=i; 101 | break; 102 | } 103 | 104 | for(int i=0; i=0) 106 | if(tree[i].value < tree[rt].value) rt=i; 107 | } 108 | tree[rt].r = &tree[position]; //setting the root of the right sub trees 109 | 110 | 111 | tree[lt].bit = false; //setting the bit of the sub trees 112 | tree[rt].bit = true; 113 | 114 | tree[position].value = (tree[lt].value + tree[rt].value); //the value is the sum of the sub trees 115 | tree[position].t1 = &tree[lt]; //setting sub tree of new node 116 | tree[position].t2 = &tree[rt]; 117 | 118 | position++; //increasing the position 119 | } 120 | 121 | 122 | /*/for(int i=0; i> ch; 137 | while(ch!='#'){ //reading the file & counting the characters 138 | node* nod; //for temporary use 139 | nod=&tree[x-1]; //the main root of tree 140 | 141 | while(nod->t1!=NULL){ 142 | //cout << ch; ///test 143 | 144 | if(ch=='0') nod = nod->t1; 145 | else nod = nod->t2; 146 | 147 | input >> ch; 148 | } 149 | fprintf(output, "%c", nod->info); 150 | //cout << nod->info << endl; ///test 151 | } 152 | 153 | fprintf(output, "#"); //indicates the end of file 154 | 155 | fclose(output); 156 | 157 | return ; 158 | } 159 | 160 | 161 | -------------------------------------------------------------------------------- /others/huffmanAlgorithm/ZipMaker.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Name: Minhas Kamal (BSSE-0509, IIT, DU) 3 | Description: This program reads a text file which ends with '#' & Zips the file. 4 | Date: 21-Oct-2013 5 | **/ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | #define S 97 //the starting char in the ascii table 15 | #define E 123 //the ending char in the ascii table 16 | 17 | 18 | ifstream input; //file reader 19 | struct node{ 20 | int value; //value of the node 21 | char info; //character that it contains 22 | bool bit; 23 | node *r, //root 24 | *t1, //left sub tree 25 | *t2; //right sub tree 26 | }; 27 | 28 | 29 | void charCounter(int* num); //counts characters in a file 30 | void treeBuilder(int* num, node* tree, int x); //creates the linked tree 31 | void zipFileWriter(int* num, node* tree); 32 | 33 | 34 | int main(){ 35 | int num[E-S]; //holds the number of characters 36 | 37 | charCounter(num); //reading a file & counting the number of different characters 38 | 39 | int x=(E-S)*2-1; 40 | node tree[x]; 41 | 42 | treeBuilder(num, tree, x); 43 | 44 | zipFileWriter(num, tree); 45 | 46 | cout << "Zip file is made.\n"; 47 | 48 | return 0; 49 | } 50 | 51 | 52 | 53 | void charCounter(int* num){ 54 | input.open("huffmanAlgorithm_Input.txt"); //opening the file 55 | 56 | if(!input.is_open()){ //when the file name is wrong 57 | printf("Sorry the file is not found!"); 58 | input.close(); 59 | return ; 60 | } 61 | 62 | for(int i=0; i> ch; 70 | while(ch!='#'){ //reading the file & counting the characters 71 | //cout << ch <<" "; ///test 72 | if(ch>=S && ch> ch; 74 | } 75 | 76 | /*/for(int i=S; i=0){ 111 | lt=i; 112 | break; 113 | } 114 | 115 | for(int i=0; i=0) 117 | if(tree[i].value < tree[lt].value) lt=i; 118 | } 119 | tree[lt].r = &tree[position]; //setting the root of the left sub trees 120 | 121 | 122 | for(int i=0; i=0){ 124 | rt=i; 125 | break; 126 | } 127 | 128 | for(int i=0; i=0) 130 | if(tree[i].value < tree[rt].value) rt=i; 131 | } 132 | tree[rt].r = &tree[position]; //setting the root of the right sub trees 133 | 134 | 135 | tree[lt].bit = false; //setting the bit of the sub trees 136 | tree[rt].bit = true; 137 | 138 | tree[position].value = (tree[lt].value + tree[rt].value); //the value is the sum of the sub trees 139 | tree[position].t1 = &tree[lt]; //setting sub tree of new node 140 | tree[position].t2 = &tree[rt]; 141 | 142 | position++; //increasing the position 143 | } 144 | 145 | 146 | /*/for(int i=0; i mystack; 175 | input >> ch; 176 | while(ch!='#'){ //reading the file once more & writing in the output file 177 | //cout << ch <<" "; ///test 178 | 179 | int i; 180 | for(i=0; ir!=NULL){ //assigning bits in the stack 187 | mystack.push(nod->bit); //bit of the node 188 | nod=nod->r; //assigning the root 189 | } 190 | 191 | 192 | while(!mystack.empty()){ //writing in the output file 193 | //cout << mystack.top(); ///test 194 | //fputc(mystack.top(), output); 195 | fprintf(output, "%d", mystack.top()); 196 | mystack.pop(); 197 | } 198 | //cout << endl; ///test 199 | 200 | input >> ch; //taking the input 201 | } 202 | fprintf(output, "#"); //indicates the end of file 203 | 204 | fclose(output); 205 | input.close(); 206 | 207 | return ; 208 | } 209 | 210 | 211 | 212 | //*/cout << "\nIts all good!!\n"; //debugging unit 213 | -------------------------------------------------------------------------------- /others/huffmanAlgorithm/huffmanAlgorithm_Input.txt: -------------------------------------------------------------------------------- 1 | caaaaaaabbbcdddddeeeeeee# -------------------------------------------------------------------------------- /playWithNumbers/factorial/BigFactorials.c: -------------------------------------------------------------------------------- 1 | /** 2 | *Name: Minhas Kamal (BSSE-0509, IIT, DU) 3 | *Date: 27.Dec.13 4 | **/ 5 | 6 | #include 7 | 8 | #define DIGIT 1000000 9 | 10 | int main() 11 | { 12 | while(1) 13 | { 14 | short int a[DIGIT+1]; 15 | int x, i; 16 | for(i=0; i0; j--) 25 | { 26 | a[j]=i%10; 27 | i=i/10; 28 | } 29 | 30 | int k=0; 31 | for(i=x-1; i>0; i--) 32 | { 33 | for(j=DIGIT; a[j]>=0; j--) 34 | { 35 | k=(i*a[j])+k; 36 | a[j]=k%10; 37 | k=k/10; 38 | } 39 | 40 | a[j]=k%10; 41 | k=k/10; 42 | a[j-1]=k; 43 | 44 | for(k=0; a[k]<1; k++) a[k]=-1; 45 | k=0; 46 | } 47 | 48 | 49 | for(i=0; a[i]<1; i++); 50 | for(; i 7 | 8 | int main() 9 | { 10 | for(; ; ) 11 | { 12 | int a[785], x, i; 13 | for(i=0; i<785; i++) a[i]=-1; 14 | scanf("%d", &x); 15 | if(x<1 || x>366) break; 16 | 17 | int j; 18 | i=x; 19 | for(j=784; i>0; j--) 20 | { 21 | a[j]=i%10; 22 | i=i/10; 23 | } 24 | 25 | int k=0; 26 | for(i=x-1; i>0; i--) 27 | { 28 | for(j=784; a[j]>=0; j--) 29 | { 30 | k=(i*a[j])+k; 31 | a[j]=k%10; 32 | k=k/10; 33 | } 34 | a[j]=k%10; 35 | k=k/10; 36 | a[j-1]=k; 37 | 38 | for(k=0; a[k]<1; k++) a[k]=-1; 39 | k=0; 40 | } 41 | 42 | int num[10]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 43 | 44 | for(i=0; i<10; i++) 45 | for(j=0; j<785; j++) 46 | if(i==a[j]) num[i]++; 47 | 48 | printf("%d! --\n", x); 49 | printf(" (0)%5d (1)%5d (2)%5d (3)%5d (4)%5d\n", num[0], num[1], num[2], num[3], num[4]); 50 | printf(" (5)%5d (6)%5d (7)%5d (8)%5d (9)%5d \n", num[5], num[6], num[7], num[8], num[9]); 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /playWithNumbers/factorial/FactorsOfFactorial.c: -------------------------------------------------------------------------------- 1 | /** 2 | *Name: Minhas Kamal (BSSE-0509, IIT, DU) 3 | *Date: 12.May.13 4 | **/ 5 | 6 | #include 7 | 8 | void varify(int x); 9 | 10 | int n[25]; 11 | 12 | int main() 13 | { 14 | for(; ; ) 15 | { 16 | int p; 17 | for(p=0; p<25; p++) n[p]=0; 18 | 19 | int i, j; 20 | scanf("%d", &i); 21 | if(i==0) break; 22 | if(i>100 || i<2) continue; 23 | 24 | 25 | for(j=2; j<=i; j++) varify(j); 26 | 27 | for(j=24; n[j]==0; j--); 28 | 29 | printf("%3d! =", i); 30 | 31 | for(i=0; i<=j; i++) 32 | { 33 | printf("%3d", n[i]); 34 | 35 | if((i+1)%15==0) printf("\n "); 36 | } 37 | printf("\n"); 38 | } 39 | 40 | 41 | return 0; 42 | } 43 | 44 | void varify(int x) 45 | { 46 | int d=x; 47 | while(d%2==0) 48 | { 49 | n[0]++; 50 | d=d/2; 51 | } 52 | 53 | int a, b, c=1; 54 | for(a=3; a<=x; a=a+2) 55 | { 56 | int z=1; 57 | for (b=3; b<=a/3; b=b+2) 58 | { 59 | if(a%b==0) 60 | { 61 | z=0; 62 | break; 63 | } 64 | } 65 | if(z==1) 66 | { 67 | d=x; 68 | while(d%a==0) 69 | { 70 | n[c]++; 71 | d=d/a; 72 | } 73 | c++; 74 | } 75 | } 76 | return ; 77 | } 78 | 79 | 80 | -------------------------------------------------------------------------------- /playWithNumbers/fibonaciiNumber/FibonaciiNumber.c: -------------------------------------------------------------------------------- 1 | /** 2 | *Name: Minhas Kamal (IIT, DU) 3 | *Date:24.Feb.2013 4 | **/ 5 | 6 | #include 7 | 8 | int main() { 9 | int a; 10 | 11 | printf("Enter number: "); 12 | scanf("%d", &a); 13 | 14 | long long int n, x = 0, y = 1, z; 15 | 16 | for (n = 1; n < a; n++) { 17 | z = y; 18 | y = x + y; 19 | x = z; 20 | if (x < 0) { 21 | printf("...\n...\nCannot calculate more than %lld!\n...\n...\n", n); 22 | return 0; 23 | } 24 | } 25 | 26 | printf("The num is: %lld\n\n", x); 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /playWithNumbers/fibonaciiNumber/FibonaciiSerise.c: -------------------------------------------------------------------------------- 1 | /** 2 | *Name: Minhas Kamal (IIT, DU) 3 | *Date:24.Feb.2013 4 | **/ 5 | 6 | #include 7 | 8 | int main() { 9 | int a, b; 10 | 11 | printf("Enter first serial num: "); 12 | scanf("%d", &a); 13 | 14 | printf("Enter second serial num: "); 15 | scanf("%d", &b); 16 | 17 | int m, n; 18 | 19 | for (m = a; m <= b; m++) { 20 | long long int x = 0, y = 1, z; 21 | 22 | for (n = 1; n < m; n++) { 23 | z = y; 24 | y = x + y; 25 | x = z; 26 | } 27 | if (x < 0) { 28 | printf("...\n...\nCannot calculate more!\n...\n...\n"); 29 | return 0; 30 | } 31 | 32 | else 33 | printf("%dth num is: %lld\n", m, x); 34 | } 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /playWithNumbers/fibonaciiNumber/FibonaciiSum.c: -------------------------------------------------------------------------------- 1 | /** 2 | *Name: Minhas Kamal (IIT, DU) 3 | *Date:24.Feb.2013 4 | **/ 5 | 6 | #include 7 | 8 | int main() { 9 | int a; 10 | 11 | printf("Enter number: "); 12 | scanf("%d", &a); 13 | 14 | long long int n, x = 0, y = 1, z, sum = 0; 15 | 16 | for (n = 1; n < a; n++) { 17 | sum = sum + y; 18 | z = y; 19 | y = x + y; 20 | x = z; 21 | if (sum < y) { 22 | printf("...\n...\nCannot calculate more than %lld!\n...\n...\n", n); 23 | return 0; 24 | } 25 | } 26 | printf("Sum is: %lld\n\n", sum); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /playWithNumbers/otherNumbers/FriendNumbers.c: -------------------------------------------------------------------------------- 1 | /** 2 | *Name: Minhas Kamal 3 | *Date:04.May.2013 4 | **/ 5 | 6 | #include 7 | 8 | 9 | int main() 10 | { 11 | long int a, b, a1, b1; 12 | printf("Enter two positive integers: "); //asking the user 13 | scanf("%ld %ld", &a, &b); 14 | a1=a; //to store the main value 15 | b1=b; //to store the main value 16 | 17 | if(a<1 || b<1) //input check 18 | { 19 | printf("Invalid input!"); 20 | exit(1); 21 | } 22 | 23 | for( ; ; ) //main operation for first integer 24 | { 25 | int af=0; 26 | for( ; a>10; a=a/10) 27 | { 28 | af=af+(a%10); 29 | } 30 | af=af+a; 31 | a=af; 32 | if(a<10) break; //breaking the loop 33 | } 34 | 35 | for( ; ; ) //main operation for second integer 36 | { 37 | int bf=0; 38 | for( ; b>10; b=b/10) 39 | { 40 | bf=bf+(b%10); 41 | } 42 | bf=bf+b; 43 | b=bf; 44 | if(b<10) break; //breaking the loop 45 | } 46 | 47 | if(a==b) printf("%ld and %ld are friends\n", a1, b1); //output 48 | else printf("%ld and %ld are not friends\n", a1, b1); 49 | 50 | return 0; 51 | } 52 | 53 | 54 | -------------------------------------------------------------------------------- /playWithNumbers/otherNumbers/PerfectNumberHaunting.c: -------------------------------------------------------------------------------- 1 | /** 2 | *Name: Minhas Kamal 3 | *Date:02.Jan.2014 4 | **/ 5 | 6 | #include 7 | 8 | int main() 9 | { 10 | long long int s; 11 | printf("Enter the starting number: "); 12 | scanf("%lld", &s); 13 | 14 | long long int f; 15 | printf("Enter the finishing number: "); 16 | scanf("%lld", &f); 17 | 18 | long long int a, b; 19 | 20 | for(a=s ; a<=f; a++) 21 | { 22 | if(a%2!=0) continue; 23 | 24 | long long int c=0; 25 | for(b=1; b<=(a/3); b++) 26 | { 27 | if(a%b==0) c=c+b; 28 | } 29 | if(a/2==c) printf("%lld, ", a); 30 | } 31 | 32 | getchar(); getchar(); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /playWithNumbers/pascalTriangle/BetterPascalTriangle.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal (DU,IIT) 3 | * Date: 09.Apr.13 4 | **/ 5 | 6 | #include 7 | 8 | int main() 9 | { 10 | int l; 11 | printf("Enter number of lines: "); 12 | scanf("%d", &l); 13 | 14 | int z, *p[100]; 15 | *p=0; 16 | *(p+1)=1; 17 | *(p+2)=0; 18 | 19 | for(z=1; z<=l; z++) 20 | { 21 | int i, j, t; 22 | 23 | for(i=0; i 7 | 8 | int main() 9 | { 10 | int a[50][50], x, i,j; 11 | 12 | printf("Enter the number of rows: "); 13 | scanf("%d", &x); 14 | 15 | for(i=0;i<=x;i++) 16 | { 17 | for(j=0;j0;j--) 34 | printf(" "); 35 | 36 | for(j=0;j 7 | 8 | int num (int x, int y); 9 | long long int factorial (int z); 10 | 11 | int main () 12 | { 13 | int l; 14 | 15 | printf("Enter number of lines: "); 16 | scanf("%d", &l); 17 | printf("\n\n\n\n"); 18 | 19 | int a, b, c; 20 | 21 | for(a=0; a0; b--)printf(" "); 23 | for(c=0; c<=a; c++){ 24 | int r = num (a, c); 25 | printf("%d ", r); 26 | } 27 | printf("\n"); 28 | } 29 | printf("\n\n\n\n"); 30 | 31 | return 0; 32 | } 33 | 34 | int num (int x, int y) 35 | { 36 | long long int m = factorial(x); 37 | long long int n = factorial(y); 38 | long long int o = factorial(x-y); 39 | 40 | int p = m/(n*o); 41 | 42 | return (p); 43 | } 44 | 45 | long long int factorial (int z) 46 | { 47 | int a; 48 | long long int b=1; 49 | for(a=1; a<=z; a++) b=b*a; 50 | 51 | return (b); 52 | } 53 | -------------------------------------------------------------------------------- /playWithNumbers/pascalTriangle/Piramid.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal (DU,IIT) 3 | * Date: 22.Feb.13 4 | **/ 5 | 6 | #include 7 | 8 | int main () 9 | { 10 | int l; 11 | 12 | printf("Enter number of lines: "); 13 | scanf("%d", &l); 14 | 15 | int a, b, c; 16 | 17 | for(a=0; a<=l; a++){ 18 | for(b=l-a; b>0; b--)printf(" "); 19 | for(c=0; c<(2*a)-1; c++)printf("|"); 20 | printf("\n"); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /playWithNumbers/pascalTriangle/RealPascalTriangle.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal (DU,IIT) 3 | * Date: 09.Apr.13 4 | **/ 5 | 6 | #include 7 | 8 | long int num (int x, int y); 9 | unsigned long long int factorial (int z); 10 | 11 | int main () 12 | { 13 | int l; 14 | 15 | printf("Enter number of lines: "); 16 | scanf("%d", &l); 17 | if(l<1) {printf("Wrong input!"); return 0;} 18 | 19 | printf("\n\n\n\n"); 20 | 21 | int a, b, c; 22 | 23 | for(a=0; a0; b--)printf(" "); 26 | for(c=0; c<=a; c++) 27 | { 28 | long int r = num (a, c); 29 | //printf("%d ", r); 30 | 31 | if(r<1) {printf("\n\n**** Memory Error! ****\n\n"); return 0;} 32 | else if(r<10) printf(" %d ", r); 33 | else if(r<100) printf(" %d ", r); 34 | else if(r<1000) printf(" %d ", r); 35 | else if(r<10000) printf(" %d ", r); 36 | else if(r<100000) printf(" %d ", r); 37 | else if(r<1000000) printf(" %d ", r); 38 | else if(r<10000000) printf(" %d", r); 39 | else if(r>=10000000) printf("%d", r); 40 | 41 | } 42 | printf("\n\n\n"); 43 | } 44 | printf("\n\n\n\n"); 45 | 46 | return 0; 47 | } 48 | 49 | long int num (int x, int y) 50 | { 51 | unsigned long long int m = factorial(x); 52 | unsigned long long int n = factorial(y); 53 | unsigned long long int o = factorial(x-y); 54 | 55 | long int p = m/(n*o); 56 | 57 | return (p); 58 | } 59 | 60 | unsigned long long int factorial (int z) 61 | { 62 | int a; 63 | unsigned long long int b=1; 64 | for(a=1; a<=z; a++) b=b*a; 65 | 66 | return (b); 67 | } 68 | 69 | -------------------------------------------------------------------------------- /playWithNumbers/primeNumber/AnotherWayOfPrimeNumberHaunting.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal 3 | * Occupation: Student (DU,IIT) 4 | * Date: Apr.13 5 | **/ 6 | 7 | #include 8 | 9 | void varify (long int m, long int n); 10 | 11 | int main () 12 | { 13 | long int s,f; //starting & finishing number 14 | 15 | printf ("Enter your starting number: "); 16 | scanf ("%ld", &s); 17 | 18 | printf ("Enter your finishing number: "); 19 | scanf ("%ld", &f); 20 | 21 | if (s>f) printf("Wrong information!\a\nstarting number is bigger.\a\n"); 22 | else if (s<2 || f<3) printf ("\aError!\a"); 23 | 24 | else{ 25 | printf("\nThe first prime is: 2\nYour requirements are given bellow:\n\n"); 26 | varify (s,f); 27 | } 28 | printf("\n\n"); 29 | 30 | return 0; 31 | } 32 | 33 | 34 | void varify (long int m, long int n) //all works are done here 35 | { 36 | long int w; 37 | if(m%2==0) w=(m+1); 38 | else w=m; 39 | 40 | for(m=w; m<=n; m=m+2){ 41 | long int y, z=1; 42 | for (y=3; y<=m/3; y=y+2){ 43 | if(m%y==0) z=0; 44 | } 45 | if (z==1)printf("%ld,\t",m); 46 | } 47 | 48 | return ; 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /playWithNumbers/primeNumber/DefinitePrimeNumberHaunting.c: -------------------------------------------------------------------------------- 1 | /** 2 | *Name: Minhas Kamal 3 | *Occupation: Student (DU,IIT) 4 | *Date: 16.May.13 5 | **/ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int main () 12 | { 13 | clock_t start, finish; 14 | 15 | long long int a, b; //domain 16 | printf("Enter your starting number: "); 17 | scanf("%lld", &a); 18 | printf("Enter your finishing number: "); 19 | scanf("%lld", &b); 20 | 21 | int k, s; 22 | printf("Enter your number of primes: "); 23 | scanf("%d", &k); 24 | printf("Enter the distance: "); 25 | scanf("%d", &s); 26 | printf("\n"); 27 | 28 | if(a>b) exit(1); 29 | long long int tup[k]; 30 | 31 | start = clock(); 32 | 33 | int i; 34 | for(i=0; i 8 | 9 | int main () 10 | { 11 | for( ; ; ) 12 | { 13 | long long int x; 14 | printf ("Enter your number: "); 15 | scanf ("%lld", &x); 16 | 17 | if(x<=0) 18 | { 19 | printf("\aError!\a\a"); 20 | return 0; 21 | } 22 | printf("\n\n"); 23 | 24 | long long int y; 25 | int z=1; 26 | 27 | for (y=2; y<=x/2; y++) 28 | { 29 | if (x%y==0) { printf ("%lld, ", y); z=0;} 30 | } 31 | 32 | if(z==1) printf("\aThis is a prime number.\a"); 33 | 34 | printf("\n\n"); 35 | 36 | int d=1; 37 | printf("Do you want to continue? 1(y) 0(n)."); 38 | scanf(" %d", &d); 39 | if(!d) break; 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /playWithNumbers/primeNumber/GreatPrimeNumberHaunting.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal 3 | * Occupation: Student (DU,IIT) 4 | * Date: 16.May.13 5 | **/ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int main () 12 | { 13 | clock_t start, finish; 14 | 15 | unsigned long long int s, f; //starting & finishing number 16 | 17 | printf ("Enter your starting number: "); 18 | scanf ("%lld", &s); 19 | 20 | printf ("Enter your finishing number: "); 21 | scanf ("%lld", &f); 22 | 23 | start = clock(); 24 | 25 | if (s>f) printf("Wrong information!\a\nstarting number is bigger.\a\n"); //starting number should be smaller than finishing number 26 | else if (s<1 || f<2) printf ("\aError!\a"); //I have done this to save more time 27 | 28 | else 29 | { 30 | if(s%2==0) s=(s+1); //I have made starting number odd, this will save time 31 | 32 | unsigned long long int x; //holds & increases starting num's value 33 | long long int y, y2; //y is the factorial of x & y2 contains square root of x 34 | char a=0; //to arrange lines 35 | char z; //here z is the flag 36 | 37 | printf("\nThe first prime is: 2\nYour requirements are given bellow:\n\n"); 38 | 39 | for(x=s; x<=f; x=x+2) //the program will work only with odd numbers 40 | { 41 | y2=sqrt(x); 42 | z=1; 43 | 44 | for (y=3; y<=y2; y=y+2) //verification loop 45 | { 46 | if(x%y==0) 47 | { 48 | z=0; 49 | break; 50 | } 51 | } 52 | 53 | if (z==1) //printing 54 | { 55 | printf("%lld,\t",x); 56 | a++; 57 | 58 | if(a%10==0) 59 | { 60 | printf("\n"); 61 | a=0; 62 | } 63 | } 64 | } 65 | printf("\n\n"); 66 | } 67 | 68 | finish = clock(); 69 | printf("TIME: %d.%.3d sec \n\n", (finish-start)/1000, (finish-start)%1000); 70 | 71 | return 0; 72 | } 73 | 74 | 75 | -------------------------------------------------------------------------------- /playWithNumbers/primeNumber/GreatestPrimeNumberHaunting.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal 3 | * Occupation: Student (DU,IIT) 4 | * Date: May.13 5 | * Comment: I have tried to make this program more time efficient, but at the same 6 | * time more complex. It finds all prime number in its domain (3 - 18,446,744,073,709,551,616). 7 | **/ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | int main () 14 | { 15 | clock_t st, fn; 16 | 17 | long long int s,f; //starting & finishing number 18 | 19 | printf ("Enter your starting number: "); 20 | scanf ("%lld", &s); 21 | 22 | printf ("Enter your finishing number: "); 23 | scanf ("%lld", &f); 24 | 25 | st=clock(); 26 | 27 | if (s>f) 28 | { //Starting number is smaller than finishing number 29 | long long int temp = s; 30 | s=f; 31 | f=temp; 32 | } 33 | 34 | if (s<2) 35 | { 36 | s=2; 37 | } 38 | 39 | if(f<3) 40 | { 41 | f=3; 42 | } 43 | 44 | else 45 | { 46 | printf("\n\n##Prime numbers are:\n\n"); 47 | if(s<3){ 48 | printf("2,\t"); 49 | } 50 | 51 | long long int x, w; 52 | 53 | if(s%2==0) w=(s+1); //I have made starting number odd, this will save time 54 | else w=s; 55 | 56 | for(x=w; x<=f; x=x+2) //The program will work only with odd numbers 57 | { 58 | int z=1; //Here z is the flag 59 | long long int y; 60 | 61 | if(x>500) 62 | { 63 | if(x%3 == 0) z=0; 64 | else if(x%5 == 0) z=0; 65 | else if(x%7 == 0) z=0; 66 | else if(x%11 == 0) z=0; 67 | else if(x%13 == 0) z=0; 68 | else if(x%17 == 0) z=0; 69 | else if(x%19 == 0) z=0; 70 | else if(x%23 == 0) z=0; 71 | else if(x%29 == 0) z=0; 72 | else if(x%31 == 0) z=0; 73 | else if(x%37 == 0) z=0; 74 | else if(x%41 == 0) z=0; 75 | else if(x%43 == 0) z=0; 76 | else if(x%47 == 0) z=0; 77 | 78 | else if(x%53 == 0) z=0; 79 | else if(x%59 == 0) z=0; 80 | else if(x%61 == 0) z=0; 81 | else if(x%67 == 0) z=0; 82 | else if(x%71 == 0) z=0; 83 | else if(x%73 == 0) z=0; 84 | else if(x%79 == 0) z=0; 85 | else if(x%83 == 0) z=0; 86 | else if(x%89 == 0) z=0; 87 | else if(x%97 == 0) z=0; 88 | 89 | else if(x%101 == 0) z=0; 90 | else if(x%103 == 0) z=0; 91 | else if(x%107 == 0) z=0; 92 | else if(x%109 == 0) z=0; 93 | else if(x%113 == 0) z=0; 94 | else if(x%127 == 0) z=0; 95 | else if(x%131 == 0) z=0; 96 | else if(x%137 == 0) z=0; 97 | else if(x%139 == 0) z=0; 98 | else if(x%149 == 0) z=0; 99 | 100 | else if(x%151 == 0) z=0; 101 | else if(x%157 == 0) z=0; 102 | else if(x%163 == 0) z=0; 103 | else if(x%167 == 0) z=0; 104 | else if(x%173 == 0) z=0; 105 | else if(x%179 == 0) z=0; 106 | else if(x%181 == 0) z=0; 107 | else if(x%191 == 0) z=0; 108 | else if(x%193 == 0) z=0; 109 | else if(x%197 == 0) z=0; 110 | else if(x%199 == 0) z=0; 111 | 112 | else if(x%211 == 0) z=0; 113 | else if(x%223 == 0) z=0; 114 | else if(x%227 == 0) z=0; 115 | else if(x%229 == 0) z=0; 116 | else if(x%233 == 0) z=0; 117 | else if(x%239 == 0) z=0; 118 | else if(x%241 == 0) z=0; 119 | 120 | else if(x%251 == 0) z=0; 121 | else if(x%257 == 0) z=0; 122 | 123 | else for (y=263; y<=sqrt(x); y=y+2) 124 | { 125 | if(x%y == 0) {z=0; break;} 126 | } 127 | } 128 | 129 | else for(y=3; y<=x/3; y=y+2) {if(x%y == 0) z=0;} 130 | 131 | 132 | if(x 6 | #include 7 | 8 | const int size=100000000; 9 | int ara[100000000]; 10 | 11 | void print_ara() 12 | { 13 | int i; 14 | for(i=2; i=size) 69 | { 70 | printf("The number should be less than %d\n", size); 71 | continue; 72 | } 73 | 74 | if(1==is_prime(n)) printf("%d is a prime number.\n", n); 75 | 76 | else printf("%d is not a prime number.\n", n); 77 | } 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /playWithNumbers/primeNumber/PrimeNumberHaunting.asm: -------------------------------------------------------------------------------- 1 | ##Writer: Minhas Kamal 2 | ##Date: 01-MAY-2014 3 | ##Function: Finds prime numbers in a certain range. 4 | 5 | #####**data**##### 6 | .data 7 | 8 | prompt: .asciiz "Enter your range: " 9 | new_line: .asciiz "\n" 10 | 11 | #####**text**##### 12 | .text 13 | 14 | main: 15 | la $a0, prompt #prompt for user input 16 | li $v0, 4 17 | syscall 18 | 19 | li $v0, 5 #take start integer 20 | syscall 21 | add $t1, $v0, $zero 22 | 23 | li $t0, 2 #see if divisible by 2 or not (even or odd) 24 | div $t4, $t1, $t0 25 | mul $t4, $t4, $t0 26 | bne $t4, $t1, odd 27 | addi $t1, $t1, 1 #if even make odd 28 | odd: 29 | 30 | li $v0, 5 #take stop integer 31 | syscall 32 | add $t2, $v0, $zero 33 | 34 | 35 | addi $t3, $t1, -2 #loop controller 36 | loop_1: 37 | addi $t3, $t3, 2 38 | 39 | ble $t3, 1, loop_1 #loopback when <=1 is inputted 40 | beq $t3, 3, isPrime #when 3 is inputted 41 | bgt $t3, $t2, exit #when >$t2 exit 42 | 43 | li $t0, 3 #loop controller #starts from 3 to last 44 | div $t4, $t3, $t0 45 | loop_2: 46 | div $t4, $t3, $t0 #see if divisible or not 47 | mul $t4, $t4, $t0 48 | beq $t4, $t3, isNotPrime 49 | 50 | addi $t0, $t0, 2 #continue the loop 51 | blt $t0, $t4, loop_2 52 | 53 | isPrime: 54 | add $a0, $t3, $zero 55 | li $v0, 1 56 | syscall 57 | la $a0, new_line 58 | li $v0, 4 59 | syscall 60 | j loop_1 61 | 62 | isNotPrime: 63 | j loop_1 64 | 65 | 66 | exit: 67 | li $v0, 10 68 | syscall 69 | -------------------------------------------------------------------------------- /playWithNumbers/primeNumber/PrimeNumberHaunting.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Name: Minhas Kamal 3 | * Occupation: Student (DU,IIT) 4 | * Date: Apr.13 5 | **/ 6 | 7 | #include 8 | 9 | int main () 10 | { 11 | long int l; 12 | printf("Enter your limit: "); 13 | scanf("%ld", &l); 14 | 15 | long int x; 16 | printf("2,\t"); 17 | for(x=3; x<=l; x=x+2){ 18 | long int y; 19 | int z=1; 20 | 21 | for (y=3; y<=x/3; y=y+2){ 22 | if(x%y==0) 23 | z=0; 24 | } 25 | if (z==1) 26 | printf("%ld,\t",x); 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /playWithNumbers/time/AgeCalculator.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Developer: Minhas Kamal (BSSE-0509, IIT, DU) 3 | * Date: 18-Aug-2013 4 | **/ 5 | 6 | // Age Calculator 7 | 8 | #include 9 | 10 | int main () 11 | { 12 | int bd, bm, by; 13 | printf("Enter your birth day (dd. mm. yy): "); 14 | scanf("%d %d %d", &bd, &bm, &by); 15 | 16 | int td, tm, ty; 17 | printf("\nEnter today's date (dd. mm. yy): "); 18 | scanf("%d %d %d", &td, &tm, &ty); 19 | 20 | int d, a=0; 21 | if(td 7 | 8 | int main() 9 | { 10 | for(; ; ) 11 | { 12 | int y; 13 | printf("Enter your year: "); 14 | scanf("%d", &y); 15 | 16 | if(y%400==0) printf("%d is a leap year.\n", y); 17 | else if(y%100==0) printf("%d is not a leap year.\n", y); 18 | else if(y%4==0) printf("%d is a leap year.\n", y); 19 | else printf("%d is not a leap year.\n", y); 20 | 21 | char ch; 22 | printf("Press 'c' to continue.\n"); 23 | fflush(stdin); 24 | scanf("%c", &ch); 25 | 26 | printf("\n"); 27 | 28 | if(ch!='c' && ch!='C') break; 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /playWithNumbers/time/Stopwatch.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Developer: Minhas Kamal (BSSE-0509, IIT, DU) 3 | * Date: Aug-2013 4 | **/ 5 | 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | printf("*#This is a stopwatch#*\n\n\n"); 12 | printf("**Press 'p' to pause.\n"); 13 | printf("**Press any key to start & to stop."); 14 | getch(); 15 | system("cls"); 16 | printf("\t\t*#This is a stopwatch#*\n\n\n"); 17 | 18 | clock_t s, n; 19 | s = clock(); 20 | 21 | while(1){ 22 | while(1){ 23 | n=clock(); 24 | printf("\r"); 25 | printf("Time-\t %d : %d : %d ", ((n-s)/1000)/60, ((n-s)/1000)%60, (n-s)%1000); 26 | if(kbhit()) break; //kbhit() does not read the character 27 | } 28 | 29 | if(getch()=='p'){ 30 | printf("\rTime-\t %d : %d : %d ", ((n-s)/1000)/60, ((n-s)/1000)%60, (n-s)%1000); 31 | getch(); 32 | } 33 | else break; 34 | 35 | s=s+(clock()-n); //split time 36 | //s=clock(); //lap time 37 | } 38 | 39 | printf("\rTime-\t %d : %d : %d ", ((n-s)/1000)/60, ((n-s)/1000)%60, (n-s)%1000); 40 | 41 | getch(); getch(); //to read the extra characters 42 | printf("\n\n\n"); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /playWithNumbers/time/TimeAfter.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Developer: Minhas Kamal (BSSE-0509, IIT, DU) 3 | * Date: Sep-2013 4 | **/ 5 | 6 | #include 7 | 8 | int main () 9 | { 10 | int s, m, h, p; 11 | printf("Enter current time (hh. mm. ss): "); 12 | scanf("%d %d %d", &h, &m, &s); 13 | printf("Is it 1='am' or 2='pm'? "); 14 | scanf("%d", &p); 15 | 16 | int as, am, ah; 17 | printf("Enter addition (hh. mm. ss): "); 18 | scanf("%d %d %d", &ah, &am, &as); 19 | 20 | int a, x=0; 21 | if(s+as >= 60){a=s+as-60; x=1;} 22 | else a=s+as; 23 | 24 | int b, y=0; 25 | if(m+am+x >= 60){b=m+am-60+x; y=1;} 26 | else b=m+am+x; 27 | 28 | int c = (h+ah+y)%12 + 1; 29 | 30 | 31 | printf("\nThe time will be: %d. %d. %d", c, b, a); 32 | 33 | if((h+ah+y)%24 < 12){ 34 | if(p==1) p=2; 35 | else p=1; 36 | } 37 | 38 | if(p==1)printf(" am\n\n"); 39 | else printf(" pm\n\n"); 40 | 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /search/aStarSearch/AStarSearch.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Developer: Minhas Kamal (BSSE-0509, IIT, DU) 3 | * Date: 06.Sep.2015 4 | * Comment: This piece of program solves 8 puzzle problem using A* search algorithm. 5 | **/ 6 | 7 | package eightPuzzle; 8 | 9 | import java.util.Random; 10 | 11 | public class AStarSearch { 12 | int SWAP_WITH_TOP=0, SWAP_WITH_RIGHT=1, SWAP_WITH_BOTTOM=2, SWAP_WITH_LEFT=3; 13 | 14 | int numberOfBoxsInRow = 3; 15 | int numberOfBoxs = numberOfBoxsInRow*numberOfBoxsInRow; // only block 0 has the capability to swap with others. 16 | 17 | int[] box; 18 | 19 | AStarSearch(){ 20 | box = new int[numberOfBoxs]; 21 | 22 | initialize(); 23 | } 24 | 25 | //////////////////////////////////////////////////////////////////////////////// 26 | 27 | private void initialize(){ 28 | System.out.println("##\t\tINITIALIZING"); 29 | 30 | for(int i=0; i0 && cyclesrecentHeuristics){ 91 | heuristics=recentHeuristics; 92 | swapDirection = i; 93 | } 94 | } 95 | } 96 | 97 | if(swapDirection == SWAP_WITH_BOTTOM){ 98 | frobiddenSwapDirection = SWAP_WITH_TOP; 99 | }else if(swapDirection == SWAP_WITH_TOP){ 100 | frobiddenSwapDirection = SWAP_WITH_BOTTOM; 101 | }else if(swapDirection == SWAP_WITH_LEFT){ 102 | frobiddenSwapDirection = SWAP_WITH_RIGHT; 103 | }else{ 104 | frobiddenSwapDirection = SWAP_WITH_LEFT; 105 | } 106 | 107 | swap(box, swapDirection); 108 | System.out.println("# Heuristics: "+heuristics); 109 | printBlocks(); 110 | 111 | cycles++; 112 | } 113 | } 114 | 115 | public int[] clone(int[] box){ 116 | int[] boxClone = new int[box.length]; 117 | for(int i=0; i=numberOfBoxs){ 152 | return false; 153 | } 154 | 155 | int temp = box[zeroBoxIndex]; 156 | box[zeroBoxIndex] = box[swapBoxIndex]; 157 | box[swapBoxIndex] = temp; 158 | 159 | return true; 160 | } 161 | 162 | public int calculateHeuristics(int[] box){ 163 | int heuristics = 0; 164 | int numericalDifference, columnDifference, rowDifference, totalDiference; 165 | 166 | for(int i=0; i 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | ifstream list; //input file stream 14 | list.open("BinarySearch_Input.txt"); 15 | //list.open(argv[1]); 16 | 17 | if(!list.is_open()){ //when file is not found 18 | cout << "File is not found!\n"; 19 | return 0; 20 | } 21 | 22 | int member; //number of integers 23 | list >> member; 24 | 25 | int myArray[member]; //stores the input data 26 | for(int i=0; i> myArray[i]; 28 | 29 | ///BinarySearch 30 | int element; 31 | cout << "Enter the element: "; 32 | cin >> element; 33 | 34 | int left=0, //left location of the myArray 35 | right=member-1, //right location of the myArray 36 | location=-1; //the location of the element 37 | 38 | while(right-left > 0){ 39 | int loc = (right+left)/2; 40 | 41 | if(myArray[loc]==element){ 42 | location=loc; 43 | break; 44 | } 45 | else if(myArray[loc]>element){ 46 | right = loc-1; 47 | } 48 | else if(myArray[loc] 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | 13 | int KMPAlgorithm(string wholeFileString, string subFileString); 14 | void FailureFunction(string subFileString, int* F); 15 | 16 | 17 | int main(int argc, char *argv[]){ 18 | //**taking the file name 19 | string fileName=""; 20 | if(argc>1){ 21 | fileName=argv[1]; 22 | }else{ 23 | cout << "Enter the file name: "; 24 | cin >> fileName; 25 | //fileName="KMPInput.txt";//test 26 | } 27 | 28 | 29 | //**opening the file 30 | ifstream input; 31 | input.open(fileName.c_str()); 32 | if(!input.is_open()){ //when the file name is wrong 33 | printf("Sorry the file is not found!"); 34 | input.close(); 35 | return 1; 36 | } 37 | 38 | 39 | //**reading file 40 | string wholeFileString=""; 41 | string subString=""; 42 | input >> wholeFileString; 43 | while(!input.eof()){ 44 | input >> subString; 45 | 46 | wholeFileString.append(" "+subString); 47 | } 48 | //cout << wholeFileString;//test 49 | input.close(); 50 | 51 | 52 | //**taking the input of sub string 53 | string subFileString=""; 54 | cout << "Enter your sub string: "; 55 | cin >> subFileString; 56 | //cout << subFileString;//test 57 | 58 | 59 | //**finding the position of sub string 60 | int startPosition=-1; 61 | 62 | //KMP Algorithm 63 | if(subFileString.length()<=wholeFileString.length()){ 64 | startPosition=KMPAlgorithm(wholeFileString, subFileString); 65 | }else{ 66 | startPosition=-2; 67 | } 68 | 69 | 70 | //**position output 71 | if(startPosition==-2){ 72 | cout << "The sub string is larger!\n\n"; 73 | }else if(startPosition==-1){ 74 | cout << "The sub string is not found!\n\n"; 75 | }else{ 76 | cout << "The position of the sub string is: " << ++startPosition << "\n\n"; 77 | } 78 | 79 | 80 | return 0; 81 | } 82 | 83 | 84 | //searching algorithm 85 | int KMPAlgorithm(string wholeFileString, string subFileString){ 86 | int position=-1; 87 | 88 | int wholeFileStringLength=wholeFileString.length(), 89 | subFileStringLength=subFileString.length(); 90 | 91 | int F[subFileStringLength]; 92 | FailureFunction(subFileString, F); 93 | 94 | /*///test 95 | int k=0; 96 | for(; k0 && subFileString[j]!=wholeFileString[i]){ 104 | j=F[j-1]; 105 | } 106 | 107 | if(subFileString[j]==wholeFileString[i]){ 108 | j++; 109 | } 110 | 111 | if(j==subFileStringLength){ 112 | position=i-subFileStringLength+1; 113 | break; 114 | } 115 | } 116 | 117 | 118 | return position; 119 | } 120 | 121 | //failure function 122 | void FailureFunction(string String, int* F){ 123 | int StringLength=String.length(); 124 | int value=0; 125 | F[0]=value; 126 | 127 | int i; 128 | for(i=1; i0 && String[value]!=String[i]){ 130 | value=F[value-1]; 131 | } 132 | 133 | if(String[value]==String[i]){ 134 | value=value+1; 135 | } 136 | 137 | F[i]=value; 138 | } 139 | 140 | 141 | return; 142 | } 143 | 144 | 145 | 146 | -------------------------------------------------------------------------------- /search/knuthMorrisPrattAlgorithm/KnuthMorrisPrattAlgorithm_Input.txt: -------------------------------------------------------------------------------- 1 | C was invented and first implemented by Dennis Ritchie on a DEC PDP-11 that used the Unix operating system. C is the result of a development process that started with an older language called BCPL. BCPL was developed by Martin Richards, and it influenced a language called B, which was invented by Ken Thompson. B led to the development of C in the 1970s. -------------------------------------------------------------------------------- /sort/quickSort/QuickSort.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Programmer: Minhas Kamal (BSSE0509,IIT,DU) 3 | * Date: 15-Nov-2013 4 | **/ 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | void quickSort(int *myArray, int left, int right); //sorts the list 12 | int qSort(int *myArray, int left, int right); //swaps & places one element at right place 13 | 14 | int main() 15 | { 16 | ifstream list; //input file stream 17 | list.open("QuickSort_Input.txt"); 18 | //list.open(argv[1]); 19 | 20 | if(!list.is_open()){ //when file is not found 21 | cout << "File is not found!\n"; 22 | return 0; 23 | } 24 | 25 | int member; //number of integers 26 | list >> member; 27 | 28 | int myArray[member]; //stores the input data 29 | for(int i=0; i> myArray[i]; 31 | 32 | int left=0, right=member-1; 33 | 34 | if(member>1) 35 | quickSort(myArray, left, right); 36 | 37 | 38 | for(int i=0; imyArray[right]){ 65 | int temp=myArray[lock]; //swapping 66 | myArray[lock]=myArray[right]; 67 | myArray[right]=temp; 68 | 69 | lock=right; 70 | } 71 | 72 | while(myArray[left]<=myArray[lock] && lock!=left) //searching from left to right 73 | left++; 74 | if(lock==left) break; 75 | if(myArray[left]>myArray[lock]){ 76 | int temp=myArray[lock]; //swapping 77 | myArray[lock]=myArray[left]; 78 | myArray[left]=temp; 79 | 80 | lock=left; 81 | } 82 | } 83 | return lock; 84 | } 85 | -------------------------------------------------------------------------------- /sort/quickSort/QuickSort_Input.txt: -------------------------------------------------------------------------------- 1 | 100 2 | 19 34 29 67 37 90 99 89 45 56 3 | 64 73 85 21 38 23 57 18 94 36 4 | 67 67 45 45 78 67 65 78 56 71 5 | 56 45 60 45 64 70 65 76 85 69 6 | 54 50 39 45 90 69 57 65 71 65 7 | 78 45 61 54 60 53 68 54 70 65 8 | 79 40 56 34 58 39 59 46 43 70 9 | 68 76 88 99 97 100 67 84 89 82 10 | 81 79 74 12 11 15 100 99 56 34 11 | 26 33 98 96 94 97 93 29 100 98 12 | -------------------------------------------------------------------------------- /thread/Input: -------------------------------------------------------------------------------- 1 | process-a 100 500 2 a 2 | process-b 200 300 1 b 3 | process-c 300 100 1 c 4 | process-d 700 100 4 d -------------------------------------------------------------------------------- /thread/priorityBased/nonpremitive/CPU.java: -------------------------------------------------------------------------------- 1 | /************************************************************************************************ 2 | * Developer: Minhas Kamal(BSSE-0509, IIT, DU) * 3 | * Date: Sep-2014 * 4 | *************************************************************************************************/ 5 | 6 | package thread.priorityBased.nonpremitive; 7 | 8 | import java.util.LinkedList; 9 | import java.util.Queue; 10 | 11 | import thread.utilClasses.Process; 12 | import thread.utilClasses.Result; 13 | 14 | public class CPU { 15 | private final int CPU_CYCLE_TIME = 100; 16 | 17 | public int time; 18 | public Queue waitingQueue = new LinkedList(); 19 | public Queue result = new LinkedList(); 20 | 21 | 22 | public Queue runProcess(Queue processes){ 23 | 24 | while(!processes.isEmpty() || !waitingQueue.isEmpty()){ 25 | 26 | while(!processes.isEmpty() && processes.element().arrivalTime<=time){ 27 | waitingQueue.add(processes.peek()); 28 | processes.remove(); 29 | } 30 | 31 | 32 | //run the program 33 | if(!waitingQueue.isEmpty()){ 34 | order(waitingQueue); 35 | 36 | waitingQueue.element().cpuTime -= CPU_CYCLE_TIME; 37 | waitingQueue.element().arrivalTime += CPU_CYCLE_TIME; 38 | 39 | 40 | if(waitingQueue.element().cpuTime<=0){ 41 | int waitingTime = time - waitingQueue.element().arrivalTime + CPU_CYCLE_TIME; 42 | result.add(new Result(waitingQueue.element().jobName, waitingQueue.element().symbol, waitingTime)); 43 | 44 | waitingQueue.remove(); 45 | }else{ 46 | result.add(new Result(waitingQueue.element().jobName, waitingQueue.element().symbol)); 47 | } 48 | 49 | }else{ //idle time, no process is waiting 50 | result.add(new Result()); 51 | } 52 | 53 | time += CPU_CYCLE_TIME; 54 | } 55 | 56 | 57 | return result; 58 | } 59 | 60 | private void order(Queue waitingQueue){ 61 | int waitingQueueLength = waitingQueue.size(); 62 | Process[] processes = new Process[waitingQueueLength]; 63 | 64 | for(int i=0; i processQueue = new LinkedList(); 20 | public Queue results = new LinkedList(); 21 | 22 | public void priorityBasedNonpremitiveImpl(String fileName){ 23 | int noOfProcess; 24 | processQueue = takeInput(fileName); 25 | noOfProcess = processQueue.size(); 26 | 27 | order(processQueue); 28 | 29 | results = new CPU().runProcess(processQueue); 30 | 31 | float totalWaitingTime = 0; 32 | Queue symbol = new LinkedList(); 33 | 34 | for(Result result: results){ 35 | //System.out.println(result.symbol + " -> " + result.waitingTime); 36 | 37 | symbol.add(result.symbol); 38 | 39 | if(result.waitingTime>0){ 40 | totalWaitingTime += result.waitingTime; 41 | } 42 | } 43 | 44 | //System.out.println("\n\n\nAverage waiting time: " + totalWaitingTime/noOfProcess); 45 | 46 | new Graph(symbol, totalWaitingTime/noOfProcess).setVisible(true); 47 | } 48 | 49 | private Queue takeInput(String fileName){ 50 | Queue processQueue = new LinkedList(); 51 | 52 | try { 53 | String string = new String(); // for temporary data store 54 | BufferedReader mainBR = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(fileName))); 55 | 56 | String jobName; 57 | int arrivalTime; 58 | int cpuTime; 59 | int priority; 60 | char symbol; 61 | 62 | int startIndex=0; 63 | int stopIndex=-1; 64 | 65 | string = mainBR.readLine(); 66 | while (string != null) { // reading step by step 67 | startIndex=stopIndex+1; 68 | stopIndex=string.indexOf(' ', startIndex); 69 | jobName = string.substring(startIndex, stopIndex); 70 | 71 | startIndex=stopIndex+1; 72 | stopIndex=string.indexOf(' ', startIndex); 73 | arrivalTime = Integer.parseInt(string.substring(startIndex, stopIndex)); 74 | 75 | startIndex=stopIndex+1; 76 | stopIndex=string.indexOf(' ', startIndex); 77 | cpuTime = Integer.parseInt(string.substring(startIndex, stopIndex)); 78 | 79 | startIndex=stopIndex+1; 80 | stopIndex=string.indexOf(' ', startIndex); 81 | priority = Integer.parseInt(string.substring(startIndex, stopIndex)); 82 | 83 | startIndex=stopIndex+1; 84 | stopIndex=string.indexOf('\n', startIndex); 85 | symbol = string.charAt(startIndex); 86 | 87 | processQueue.add(new Process(jobName, arrivalTime, cpuTime, priority, symbol)); 88 | 89 | string = mainBR.readLine(); 90 | } 91 | 92 | mainBR.close(); // closing the file 93 | 94 | }catch (Exception e) { 95 | e.printStackTrace(); 96 | } 97 | 98 | /*///test 99 | processQueue.add(new Process("process-1", 100, 400, 2, '1')); 100 | processQueue.add(new Process("process-2", 200, 600, 1, '2')); 101 | processQueue.add(new Process("process-3", 100, 100, 1, '3')); 102 | processQueue.add(new Process("process-4", 300, 300, 3, '4')); 103 | /**/ 104 | return processQueue; 105 | } 106 | 107 | private void order(Queue processQueue){ 108 | int waitingQueueLength = processQueue.size(); 109 | Process[] processes = new Process[waitingQueueLength]; 110 | 111 | for(int i=0; i processes[j].arrivalTime){ 120 | tempProcess = processes[i]; 121 | processes[i] = processes[j]; 122 | processes[j] = tempProcess; 123 | } 124 | } 125 | } 126 | 127 | for(int i=0; i waitingQueue = new LinkedList(); 19 | public Queue result = new LinkedList(); 20 | 21 | 22 | public Queue runProcess(Queue processes){ 23 | 24 | while(!processes.isEmpty() || !waitingQueue.isEmpty()){ 25 | 26 | //process starts 27 | while(!processes.isEmpty() && processes.element().arrivalTime<=time){ 28 | waitingQueue.add(processes.peek()); 29 | processes.remove(); 30 | order(waitingQueue); 31 | } 32 | 33 | 34 | //run the program 35 | if(!waitingQueue.isEmpty()){ 36 | waitingQueue.element().cpuTime -= CPU_CYCLE_TIME; 37 | waitingQueue.element().arrivalTime += CPU_CYCLE_TIME; 38 | 39 | 40 | if(waitingQueue.element().cpuTime<=0){ //process ends 41 | int waitingTime = time - waitingQueue.element().arrivalTime + CPU_CYCLE_TIME; 42 | result.add(new Result(waitingQueue.element().jobName, waitingQueue.element().symbol, waitingTime)); 43 | 44 | waitingQueue.remove(); 45 | }else{ 46 | result.add(new Result(waitingQueue.element().jobName, waitingQueue.element().symbol)); 47 | } 48 | }else{ //idle time, no process is waiting 49 | result.add(new Result()); 50 | } 51 | 52 | time += CPU_CYCLE_TIME; 53 | } 54 | 55 | 56 | return result; 57 | } 58 | 59 | private void order(Queue waitingQueue){ 60 | int waitingQueueLength = waitingQueue.size(); 61 | Process[] processes = new Process[waitingQueueLength]; 62 | 63 | for(int i=0; i processQueue = new LinkedList(); 21 | public Queue results = new LinkedList(); 22 | 23 | /** 24 | * 25 | * @param fileName 26 | */ 27 | public void priorityBasedPremitiveImpl(String fileName){ 28 | int noOfProcess; 29 | processQueue = takeInput(fileName); 30 | noOfProcess = processQueue.size(); 31 | 32 | order(processQueue); 33 | 34 | results = new CPU().runProcess(processQueue); 35 | 36 | float totalWaitingTime = 0; 37 | Queue symbol = new LinkedList(); 38 | 39 | for(Result result: results){ 40 | //System.out.println(result.symbol + " -> " + result.waitingTime); 41 | 42 | symbol.add(result.symbol); 43 | 44 | if(result.waitingTime>0){ 45 | totalWaitingTime += result.waitingTime; 46 | } 47 | } 48 | 49 | //System.out.println("\n\n\nAverage waiting time: " + totalWaitingTime/noOfProcess); 50 | 51 | new Graph(symbol, totalWaitingTime/noOfProcess).setVisible(true); 52 | } 53 | 54 | private Queue takeInput(String fileName){ 55 | Queue processQueue = new LinkedList(); 56 | 57 | try { 58 | String string = new String(); // for temporary data store 59 | BufferedReader mainBR = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(fileName))); 60 | 61 | String jobName; 62 | int arrivalTime; 63 | int cpuTime; 64 | int priority; 65 | char symbol; 66 | 67 | int startIndex=0; 68 | int stopIndex=-1; 69 | 70 | string = mainBR.readLine(); 71 | while (string != null) { // reading step by step 72 | startIndex=stopIndex+1; 73 | stopIndex=string.indexOf(' ', startIndex); 74 | jobName = string.substring(startIndex, stopIndex); 75 | 76 | startIndex=stopIndex+1; 77 | stopIndex=string.indexOf(' ', startIndex); 78 | arrivalTime = Integer.parseInt(string.substring(startIndex, stopIndex)); 79 | 80 | startIndex=stopIndex+1; 81 | stopIndex=string.indexOf(' ', startIndex); 82 | cpuTime = Integer.parseInt(string.substring(startIndex, stopIndex)); 83 | 84 | startIndex=stopIndex+1; 85 | stopIndex=string.indexOf(' ', startIndex); 86 | priority = Integer.parseInt(string.substring(startIndex, stopIndex)); 87 | 88 | startIndex=stopIndex+1; 89 | stopIndex=string.indexOf('\n', startIndex); 90 | symbol = string.charAt(startIndex); 91 | 92 | processQueue.add(new Process(jobName, arrivalTime, cpuTime, priority, symbol)); 93 | 94 | string = mainBR.readLine(); 95 | } 96 | 97 | mainBR.close(); // closing the file 98 | 99 | }catch (Exception e) { 100 | e.printStackTrace(); 101 | } 102 | 103 | /*///test 104 | processQueue.add(new Process("process-1", 100, 400, 2, '1')); 105 | processQueue.add(new Process("process-2", 200, 600, 1, '2')); 106 | processQueue.add(new Process("process-3", 100, 100, 1, '3')); 107 | processQueue.add(new Process("process-4", 300, 300, 3, '4')); 108 | /**/ 109 | return processQueue; 110 | } 111 | 112 | private void order(Queue processQueue){ 113 | int waitingQueueLength = processQueue.size(); 114 | Process[] processes = new Process[waitingQueueLength]; 115 | 116 | for(int i=0; i processes[j].arrivalTime){ 125 | tempProcess = processes[i]; 126 | processes[i] = processes[j]; 127 | processes[j] = tempProcess; 128 | } 129 | } 130 | } 131 | 132 | for(int i=0; i waitingQueue = new LinkedList(); 19 | public Queue result = new LinkedList(); 20 | 21 | 22 | public Queue runProcess(Queue processes){ 23 | 24 | while(!processes.isEmpty() || !waitingQueue.isEmpty()){ 25 | 26 | //process starts 27 | while(!processes.isEmpty() && processes.element().arrivalTime<=time){ 28 | waitingQueue.add(processes.peek()); 29 | processes.remove(); 30 | } 31 | 32 | 33 | //run the program 34 | if(!waitingQueue.isEmpty()){ //when there is process to be processed 35 | 36 | order(waitingQueue); 37 | 38 | waitingQueue.element().cpuTime -= CPU_CYCLE_TIME; 39 | waitingQueue.element().arrivalTime += CPU_CYCLE_TIME; 40 | 41 | if(waitingQueue.element().cpuTime<=0){ //process ends 42 | int waitingTime = time - waitingQueue.element().arrivalTime + CPU_CYCLE_TIME; 43 | result.add(new Result(waitingQueue.element().jobName, waitingQueue.element().symbol, waitingTime)); 44 | 45 | waitingQueue.remove(); 46 | }else{ 47 | result.add(new Result(waitingQueue.element().jobName, waitingQueue.element().symbol)); 48 | } 49 | 50 | }else{ //idle time, no process is waiting 51 | result.add(new Result()); 52 | } 53 | 54 | time += CPU_CYCLE_TIME; 55 | } 56 | 57 | 58 | return result; 59 | } 60 | 61 | private void order(Queue waitingQueue){ 62 | int waitingQueueLength = waitingQueue.size(); 63 | Process[] processes = new Process[waitingQueueLength]; 64 | 65 | for(int i=0; i processes[j].cpuTime){ 74 | tempProcess = processes[i]; 75 | processes[i] = processes[j]; 76 | processes[j] = tempProcess; 77 | } 78 | } 79 | } 80 | 81 | for(int i=0; i processQueue = new LinkedList(); 20 | public Queue results = new LinkedList(); 21 | 22 | public void shortestJobFirstNonpremitiveImpl(String fileName){ 23 | int noOfProcess; 24 | processQueue = takeInput(fileName); 25 | noOfProcess = processQueue.size(); 26 | 27 | order(processQueue); 28 | 29 | results = new CPU().runProcess(processQueue); 30 | 31 | float totalWaitingTime = 0; 32 | Queue symbol = new LinkedList(); 33 | 34 | for(Result result: results){ 35 | //System.out.println(result.symbol + " -> " + result.waitingTime); 36 | 37 | symbol.add(result.symbol); 38 | 39 | if(result.waitingTime>0){ 40 | totalWaitingTime += result.waitingTime; 41 | } 42 | } 43 | 44 | //System.out.println("\n\n\nAverage waiting time: " + totalWaitingTime/noOfProcess); 45 | 46 | new Graph(symbol, totalWaitingTime/noOfProcess).setVisible(true); 47 | } 48 | 49 | private Queue takeInput(String fileName){ 50 | Queue processQueue = new LinkedList(); 51 | 52 | try { 53 | String string = new String(); // for temporary data store 54 | BufferedReader mainBR = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(fileName))); 55 | 56 | String jobName; 57 | int arrivalTime; 58 | int cpuTime; 59 | int priority; 60 | char symbol; 61 | 62 | int startIndex=0; 63 | int stopIndex=-1; 64 | 65 | string = mainBR.readLine(); 66 | while (string != null) { // reading step by step 67 | startIndex=stopIndex+1; 68 | stopIndex=string.indexOf(' ', startIndex); 69 | jobName = string.substring(startIndex, stopIndex); 70 | 71 | startIndex=stopIndex+1; 72 | stopIndex=string.indexOf(' ', startIndex); 73 | arrivalTime = Integer.parseInt(string.substring(startIndex, stopIndex)); 74 | 75 | startIndex=stopIndex+1; 76 | stopIndex=string.indexOf(' ', startIndex); 77 | cpuTime = Integer.parseInt(string.substring(startIndex, stopIndex)); 78 | 79 | startIndex=stopIndex+1; 80 | stopIndex=string.indexOf(' ', startIndex); 81 | priority = Integer.parseInt(string.substring(startIndex, stopIndex)); 82 | 83 | startIndex=stopIndex+1; 84 | stopIndex=string.indexOf('\n', startIndex); 85 | symbol = string.charAt(startIndex); 86 | 87 | processQueue.add(new Process(jobName, arrivalTime, cpuTime, priority, symbol)); 88 | 89 | string = mainBR.readLine(); 90 | } 91 | 92 | mainBR.close(); // closing the file 93 | 94 | }catch (Exception e) { 95 | e.printStackTrace(); 96 | } 97 | 98 | /*///test 99 | processQueue.add(new Process("process-1", 100, 400, 2, '1')); 100 | processQueue.add(new Process("process-2", 200, 600, 1, '2')); 101 | processQueue.add(new Process("process-3", 100, 100, 1, '3')); 102 | processQueue.add(new Process("process-4", 300, 300, 3, '4')); 103 | /**/ 104 | return processQueue; 105 | } 106 | 107 | private void order(Queue processQueue){ 108 | int waitingQueueLength = processQueue.size(); 109 | Process[] processes = new Process[waitingQueueLength]; 110 | 111 | for(int i=0; i processes[j].arrivalTime){ 120 | tempProcess = processes[i]; 121 | processes[i] = processes[j]; 122 | processes[j] = tempProcess; 123 | } 124 | } 125 | } 126 | 127 | for(int i=0; i waitingQueue = new LinkedList(); 19 | public Queue result = new LinkedList(); 20 | 21 | 22 | public Queue runProcess(Queue processes){ 23 | 24 | while(!processes.isEmpty() || !waitingQueue.isEmpty()){ 25 | //process starts 26 | while(!processes.isEmpty() && processes.element().arrivalTime<=time){ 27 | waitingQueue.add(processes.peek()); 28 | processes.remove(); 29 | } 30 | 31 | //run the program 32 | if(!waitingQueue.isEmpty()){ 33 | order(waitingQueue); 34 | 35 | waitingQueue.element().cpuTime -= CPU_CYCLE_TIME; 36 | waitingQueue.element().arrivalTime += CPU_CYCLE_TIME; 37 | 38 | if(waitingQueue.element().cpuTime<=0){ //process ends 39 | int waitingTime = time - waitingQueue.element().arrivalTime + CPU_CYCLE_TIME; 40 | result.add(new Result(waitingQueue.element().jobName, waitingQueue.element().symbol, waitingTime)); 41 | 42 | waitingQueue.remove(); 43 | }else{ 44 | result.add(new Result(waitingQueue.element().jobName, waitingQueue.element().symbol)); 45 | } 46 | }else{ //idle time, no process is waiting 47 | result.add(new Result()); 48 | } 49 | 50 | time += CPU_CYCLE_TIME; 51 | } 52 | 53 | 54 | return result; 55 | } 56 | 57 | private void order(Queue waitingQueue){ 58 | int waitingQueueLength = waitingQueue.size(); 59 | Process[] processes = new Process[waitingQueueLength]; 60 | 61 | for(int i=0; i processes[j].cpuTime){ 70 | tempProcess = processes[i]; 71 | processes[i] = processes[j]; 72 | processes[j] = tempProcess; 73 | } 74 | } 75 | } 76 | 77 | for(int i=0; i processQueue = new LinkedList(); 20 | public Queue results = new LinkedList(); 21 | 22 | public void shortestJobFirstPremitiveImpl(String fileName){ 23 | int noOfProcess; 24 | processQueue = takeInput(fileName); 25 | noOfProcess = processQueue.size(); 26 | 27 | order(processQueue); 28 | 29 | results = new CPU().runProcess(processQueue); 30 | 31 | float totalWaitingTime = 0; 32 | Queue symbol = new LinkedList(); 33 | 34 | for(Result result: results){ 35 | //System.out.println(result.symbol + " -> " + result.waitingTime); 36 | 37 | symbol.add(result.symbol); 38 | 39 | if(result.waitingTime>0){ 40 | totalWaitingTime += result.waitingTime; 41 | } 42 | } 43 | 44 | //System.out.println("\n\n\nAverage waiting time: " + totalWaitingTime/noOfProcess); 45 | 46 | new Graph(symbol, totalWaitingTime/noOfProcess).setVisible(true); 47 | } 48 | 49 | private Queue takeInput(String fileName){ 50 | Queue processQueue = new LinkedList(); 51 | 52 | try { 53 | String string = new String(); // for temporary data store 54 | BufferedReader mainBR = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(fileName))); 55 | 56 | String jobName; 57 | int arrivalTime; 58 | int cpuTime; 59 | int priority; 60 | char symbol; 61 | 62 | int startIndex=0; 63 | int stopIndex=-1; 64 | 65 | string = mainBR.readLine(); 66 | while (string != null) { // reading step by step 67 | startIndex=stopIndex+1; 68 | stopIndex=string.indexOf(' ', startIndex); 69 | jobName = string.substring(startIndex, stopIndex); 70 | 71 | startIndex=stopIndex+1; 72 | stopIndex=string.indexOf(' ', startIndex); 73 | arrivalTime = Integer.parseInt(string.substring(startIndex, stopIndex)); 74 | 75 | startIndex=stopIndex+1; 76 | stopIndex=string.indexOf(' ', startIndex); 77 | cpuTime = Integer.parseInt(string.substring(startIndex, stopIndex)); 78 | 79 | startIndex=stopIndex+1; 80 | stopIndex=string.indexOf(' ', startIndex); 81 | priority = Integer.parseInt(string.substring(startIndex, stopIndex)); 82 | 83 | startIndex=stopIndex+1; 84 | stopIndex=string.indexOf('\n', startIndex); 85 | symbol = string.charAt(startIndex); 86 | 87 | processQueue.add(new Process(jobName, arrivalTime, cpuTime, priority, symbol)); 88 | 89 | string = mainBR.readLine(); 90 | } 91 | 92 | mainBR.close(); // closing the file 93 | 94 | }catch (Exception e) { 95 | e.printStackTrace(); 96 | } 97 | 98 | /*///test 99 | processQueue.add(new Process("process-1", 100, 400, 2, '1')); 100 | processQueue.add(new Process("process-2", 200, 600, 1, '2')); 101 | processQueue.add(new Process("process-3", 100, 100, 1, '3')); 102 | processQueue.add(new Process("process-4", 300, 300, 3, '4')); 103 | /**/ 104 | return processQueue; 105 | } 106 | 107 | private void order(Queue processQueue){ 108 | int waitingQueueLength = processQueue.size(); 109 | Process[] processes = new Process[waitingQueueLength]; 110 | 111 | for(int i=0; i processes[j].arrivalTime){ 120 | tempProcess = processes[i]; 121 | processes[i] = processes[j]; 122 | processes[j] = tempProcess; 123 | } 124 | } 125 | } 126 | 127 | for(int i=0; i symbols, float f) { 31 | parts = symbols.size(); 32 | 33 | initialComponent(symbols, f); 34 | } 35 | 36 | 37 | /** 38 | * Method for Initializing all the GUI variables, placing them all to specific space on the frame and adding action 39 | * listener to them. Also specifies criteria of the main frame. 40 | */ 41 | private void initialComponent(Queue symbols, float f) { 42 | //** 43 | // Initialization #*******I*******# 44 | //** 45 | jPanelMain = new JPanel(); 46 | jPanelGraph = new JPanel[parts]; 47 | jLabel = new JLabel(); 48 | //other variables 49 | Queue uniqueSymbols = new LinkedList(); 50 | // End of Initialization #_______I_______# 51 | 52 | //** 53 | // Setting Bounds and Attributes of the Elements #*******S*******# 54 | //** 55 | jPanelMain.setBounds(0, 0, LENGTH+40, 250); 56 | jPanelMain.setBackground(new Color(-1)); 57 | jPanelMain.setLayout(null); 58 | 59 | int partLength = LENGTH/parts; 60 | for(int i=0; i symbol = new LinkedList(); 118 | symbol.add('a');symbol.add('a');symbol.add('b');symbol.add('c'); 119 | symbol.add('a');symbol.add('c');symbol.add('c');symbol.add('d'); 120 | symbol.add('a');symbol.add('b');symbol.add('c');symbol.add('e'); 121 | symbol.add('f');symbol.add('f');symbol.add('f');symbol.add('c'); 122 | symbol.add('a');symbol.add('g');symbol.add('g');symbol.add('d'); 123 | symbol.add('h');symbol.add('h');symbol.add('h');symbol.add('e'); 124 | symbol.add('a');symbol.add('a');symbol.add('c');symbol.add('c'); 125 | symbol.add('i');symbol.add('j');symbol.add('k');symbol.add('i'); 126 | symbol.add('j');symbol.add('j');symbol.add('j');symbol.add('k'); 127 | 128 | Graph gui = new Graph(symbol, 100); 129 | gui.setVisible(true); 130 | } 131 | 132 | //** 133 | // Auxiliary Methods #********AM*******# 134 | //** 135 | 136 | private int getPosition(char symbol, Queue uniqueSymbols){ 137 | int position = -1; 138 | 139 | int p = 0; 140 | for(Character uniqueSymbol: uniqueSymbols){ 141 | if(symbol==uniqueSymbol){ 142 | position=p; 143 | break; 144 | } 145 | p++; 146 | } 147 | 148 | return position; 149 | } 150 | 151 | private Color getColor(int i){ 152 | i *= 150; 153 | 154 | int r = i%256; 155 | int g = i%200+i%56; 156 | int b = i%100+i%156; 157 | 158 | Color color = new Color(r, g, b); 159 | return color; 160 | } 161 | // End of Auxiliary Methods #________AM_______# 162 | } 163 | -------------------------------------------------------------------------------- /thread/utilClasses/Process.java: -------------------------------------------------------------------------------- 1 | /************************************************************************************************ 2 | * Developer: Minhas Kamal(BSSE-0509, IIT, DU) * 3 | * Date: Sep-2014 * 4 | *************************************************************************************************/ 5 | 6 | package thread.utilClasses; 7 | 8 | public class Process { 9 | public String jobName; 10 | public int arrivalTime; 11 | public int cpuTime; 12 | public int priority; 13 | public char symbol; 14 | 15 | public Process(){ 16 | this.jobName = ""; 17 | this.arrivalTime = 0; 18 | this.cpuTime = 0; 19 | this.priority = 1; 20 | this.symbol = '0'; 21 | } 22 | 23 | public Process(String jobName, int arrivalTime, int cpuTime, char symbol){ 24 | this.jobName = jobName; 25 | this.arrivalTime = arrivalTime; 26 | this.cpuTime = cpuTime; 27 | this.priority = 1; 28 | this.symbol = symbol; 29 | } 30 | 31 | public Process(String jobName, int arrivalTime, int cpuTime, int priority, char symbol){ 32 | this.jobName = jobName; 33 | this.arrivalTime = arrivalTime; 34 | this.cpuTime = cpuTime; 35 | this.priority = priority; 36 | this.symbol = symbol; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /thread/utilClasses/Result.java: -------------------------------------------------------------------------------- 1 | /************************************************************************************************ 2 | * Developer: Minhas Kamal(BSSE-0509, IIT, DU) * 3 | * Date: Sep-2014 * 4 | *************************************************************************************************/ 5 | 6 | package thread.utilClasses; 7 | 8 | public class Result { 9 | public String jobName; 10 | public char symbol; 11 | public float waitingTime; 12 | 13 | public Result() { 14 | this.jobName = "EMPTY"; 15 | this.symbol = ' '; 16 | waitingTime = -1; 17 | } 18 | 19 | public Result(String jobName, char symbol){ 20 | this.jobName = jobName; 21 | this.symbol = symbol; 22 | waitingTime = -1; 23 | } 24 | 25 | public Result(String jobName, char symbol, float waitingTime){ 26 | this.jobName = jobName; 27 | this.symbol = symbol; 28 | this.waitingTime = waitingTime; 29 | } 30 | } 31 | --------------------------------------------------------------------------------