Category Archives: Programming

My first experience of problem setting. It feels good when you see people think about your problem and come up with better solutions than yours 😛
This problem is about math and primes. The link is this.

Kruskal’s algorithm is the best and probably fastest option if you’re urging to form a subgraph of a graph connecting all nodes. Some implementations are shown.Minimum Spanning Tree
To form an MST(Minimum Spanning Tree) follow this procedure,

1. Begin with a connected graph G.
2. E is the set of all edges of different weights, from G.
3. Sort edges of E in the ascending order of their weights.
4. Go through each member(edge) of E and see if the nodes of that edge is already connected or not.
5. If they aren’t connected, connect them and include that member of E(edge) int MST.
6. Continue this process untill you’ve got n-1 edges(in case of n nodes).

Second Best Minimum Spanning Tree
There may be some other better procedure to find the second best MST, but this is the one I’ve used.
1. First form an MST of graph G, and mark the edges which formes the MST.
2. Then for each member edge of the MST, Form another MST without using that particular member.
3. Thus, u need to form n-1 (in case of n nodes) ‘another’ MSTs and consider the minimun among them.

There are many notable algorithms to calculate the shortest path between vertices in a graph. Most are based on single source to a set of destination vertices. Examples of such famous algorithms include Dijkstra’s, Bellman-Ford and even Breadth First Search for weightless graphs. However, sometimes we wish to calculate the shortest paths between all pairs of vertices. An easy way to calculate this is to loop through each possible pair of vertices and run a Dijkstra through it. However, there exists a more efficient way to calculate this in n^3 time complexity.

Floyd-Warshall algorithm, a formula consisting of the core is very simple. A, B, C, I suppose we have 3 points. In this case, the shortest distance between A and C (distance (A, C), distance (A, B) + distance (B, C)) is up. Floyd-Warshall’ı need to apply to a table. The size of this table, depending on the number of points to be determined. We got one point, the table size N * N N

int[,] table = new int[5,5];

On the basis of the algorithm used for the function of the input for this table are the values at the maximum. The distance of each point of self-0, the distance to other points, we will begin by accepting the infinite.

Second, to keep the connection between the points and to build the matrix of contiguity for questioning. If you have an edge from one point to another entry in the table that would give a value. For example, point 3 and point 4, one side has 2 points. Therefore, our statements [2,3] = [2.4] = 1 = [3.2] = [4.2]. Grafımız no way we obtain for the diagonal matrix based on contiguity should be symmetrical. 1 to 4, because to have the edge in the 4 to 1, this also means there are also edges. Our example is a way you could tell it had graphs. After this, the final version of the table will be as follows:

atod function’s work is identical to atoi‘s. But this one is for doubles

// for texting/ example purposes
#include <iostream> // for cout and endl
#include <cmath> // for pow
using namespace std; // so we don't have to do std::coud or std::endl
//end testing/ example purposes
char* substr(char* str, int start, int end){
char* a = new char[1+(end-start)]; // we need a new char array for the return
for(int i=start; i<end; i++){ // loop through the string
a[i-start] = str[i]; // set the characters in the new char array to those from the old one compensating for the substring
}
a[end-start] = '\0'; // add the null character, so we can output
return a; // return
}
double atod(char* a){
double retVal = atoi(a); // start off getting the number, assuming it is a valid string to use atoi on.
int start = 0;
int end = 0;
for(int i=0; a[i] != '\0'; i++){ // loop through the string to find the positions of the decimal portion, if there is one
if(a[i] == '.' && start == 0){
start = i+1; // set the start position to 1 more than the current (avoids a char as the first character - we want a digit)
}
else if(start != 0 && (a[i] < '0' || a[i] > '9')){ // make sure that start is set and that we aren't looking at digits
end = i; // if so, set the end location for the substring
break; // we don't need to continue anymore - break out of the loop
}
}
if(end > start){ // avoids substring problems.
char* decimal = substr(a, start, end); // get the string that is after the decimal point
int dec = atoi(decimal); // make it an integer
int power = end-start; // find the power of 10 we need to divide by
retVal += ((double)dec)/(pow(10.0, (double)power)); // divide and add to the return value (thus making it a true double)
}
return retVal; // return - simple enough
}
// for testing/ example purposes
int main(){
cout << atod("127.02501 test") << endl; // test it out on a valid character stream
return 0;
}
// end testing/ example purposes

void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *));
■base — is a pointer to the beginning of data array
■nel — is a number of elements
■width — is a size of each element (in bytes)
■compar — is a callback function (pointer to function), which does comparison and returns positive or negative integer depending on result.

This example contains three separate functions sort_integers_example(), sort_cstrings_example() and sort_structs_example().

I had been using www.codepad.org whenever I wanted to run a piece of code and didn’t have the required compiler/interpreter in the system I was using. But the main disadvantage with this was that you cannot read input from the user while using this site – scanf(), cin, , etc. (in C, C++, Perl respectively) are promptly ignored. This was often an irritation since I’d have to modify my program to not take an input before testing there, which might not be an easy process (especially given complex data structures).

Today, I came across ideone.com, which is very similar to the above, except that it allows providing input to the program! Yay! You type (or paste) the program in a text area as in codepad, choose your language from the list, and then click on “click here to paste input (stdin) or additional note”. Whatever you type here becomes the input to your program. That’s a neat solution to the problem of providing input to remotely-evaluated programs.
And it supports “more than 40 languages”, including Assembler, Common Lisp, and even Whitespace (of course the boring old C, C++, Java, Perl, Python, etc. are there too). It has other goodies like syntax highlighting for all these languages, ‘private’ pasting for times when you don’t want others to see the code, etc. The site is quite very well done.

While we’re in the topic, let me also add two more online code evaluation tools (compilers/interpreters):

* Lord of the REPLs: This is a Read-Eval-Print-Loop from Google, where each line you enter is evaluated immediately. Despite the fancy “Lord” name and the Google brand, I haven’t found this very useful mainly because: it doesn’t preserve function definitions or variables’ values after you evaluate them, unlike other REPLs. This makes it hard to run anything more than trivial “hello world” programs. Also, I found it quite buggy at times.

* “Compile & Execute”: This is somewhat similar to ideone, allowing you to enter your own input and having a choice of a similar number of languages. However, it doesn’t have a ‘private’ pasting option, and has syntax highlighting for only a few languages. This leads to weird highlighting at times: if you choose to paste Perl code, the Highlight choice remains in cpp (which is the default), so your Perl code gets highlighted as C++ code! Sloppy design like this makes me think this site might not be very well designed.

These two are the only generic code evaluation tools I could find. If you find that none of these do it for you, try googling for something specific to your language. For example, for Python, searching for ‘python online (IDE OR compiler OR interpreter)’ gives a lot of good results.

If you know some other online code evaluation tool, or have experience with any of the above, do share it in the comments. Thanks!