C programming language, shortest path - c

I'm am writing a code to find the shortest distance between two points. My code is working perfect till now. I mean it finds the distance and the path that they should pass. I need to print this information, but I should make a print function. The way it works is something like that: For example initial point is 4 and final is 13.
I should come up with an algorithm that check their intermediate points. Let's say between 4 & 13 there is point : 7
4--7--13 Now I need to check every point between them like:
4--6--7--9--13 To be more specific it will check if there is a point between 4-6 and 6-7 and 7-9 and 9-13. So next in the next iteration it may be formed another list like:
4--2--6--7--5--9--17--13 Now let's say that there will not be any intermediate value between them. And that is what I should print. I really would appreciate any help, suggestion that you may give to me

The Warshall-Floyd algorithm (used by the OP), has a version which is able to determine the path in addition to the distance between nodes of a graph:
Floyd-Warshall algorithm with path-reconstruction
However, it must be noted that this is not the best possible algorithm to solve the shortest-path problem.

This sounds like recursion would be the best way to do this. If it already can find the shortest path, im assuming you have a function written to find the shortest path between 2 points. Maybe you could recursively break down the list, find the shortest path and append that point to a list.
Edit, sorry i misread your question, you need to find the midpoint. Pass a recursive function the whole list of points and find a midpoint. If one exists, add it to a list. If there is no midpoint dont append anything. Continue calling this function until you come to the base case, which should be 1 or 2 points in the list

Related

Make BFS function on graph to get the shortest path

I'm currently preparing an exam and I'm having some troubles implementing Breadth - First -Search algorithm about graphs.
Basically I found an exercise where I've been asked to find the shortest path between a start vertex v1 and an ending vertex v2 using the BFS algorithm. Now, on the web it's easy to find some BFS implementations in C but I'm not able to understand how to adapt it to find only the shortest path.
I've already made all the functions about push and pop in stack list and graph making from user input I'm just asking to one kind person if he can write here an example code in C of the BFS function to solve my problem.
Thank you and have a nice day!
BFS is iterative, and each iteration sets the parent property of each node right? So if one of the vertex-labels in the current iteration matches your target (v2) then you are done. Next you just trace the parent property back to your start (v1)

Best Search-Algorithm to search an Input String

I have a theoretical question.
I'm trying to find the best search algorithm to deal with the following problem: I want to process a input String, the input String can have up to 7 independent and valid arguments (parameters), which start different segments in my code. All parameters can appear at any position inside the input, which means the input does not have a order.
My first idea was to look at the input string and do a strcmp for all individual valid inputs at every position inside the string, which would mean that I would basically search the string until I find a match or reach the end of the input. However, this would have very bad runtime latency as I would iterate the input with n!.
I'm wondering if there is a better way to search the input, maybe something where I can decrease the sample size after a valid input has been found, so I dont have to look at this position again. I would be glad if someone could help me to find a search-algorithm with a better runtime efficiency.
As far as I know, its not possible to decrease a stack size. I guess the only way to improve the efficiency is to skip positions inside the stack, based on a bool value? Every position that I can skip drastically improves the runtime, as it would have to be compared with all 7 inputs.
Thanks for reading :)
I'd bet that your libc implementation does this quite well ;)
strstr() is the function you are looking for, go check this man : http://manpagesfr.free.fr/man/man3/strstr.3.html

Find if a line segment is a sub segment of another line

How do I find if a certain line is a sub segment of another line?
Line 1: (2,2) and (20,20)
Line 2: (5,5) and (15,15)
I know how to do this mathematically but having a hard time coding it in c. I found out that the slopes are equal but cant find a way to check that the lines are part of the same line and not just parallel.
This seems to be homework, so I will only sketch an answer. You will have to code it yourself.
First, are the slopes the same? If not, they only have one point in common; stop. Assuming the coordinates are integers, you can avoid floating-point errors by computing each dx and dy, and cross-multiplying (h/t to Weather Vane’s comment above).
Second, do the extended lines have a point in common? Can do this by finding y-intercepts or various other ways. If not, they’re parallel; stop.
Having reached this point, we know the segments are on the same line, so now we have to test the intervals. If they’re horizontal, look at x; if vertical, look at y; if slanted, pick one coordinate, either one will work.
Sort each pair of endpoints to get a and b such that a < b. Then, simply compare the leftmost endpoints of each segment, and the rightmost. Remember that either or both might be equal.

Reconstruct version control from set of files

I am looking after an approach for the following task:
given a set of files that are highly similar (I am using Fuzzy hashing here), I would like to know if there is an algorithm that allows to label those files with a version number. The output should return the sequential order of when those files have been generated.
The reason is I have to re-organize data of a team who were not familiar with version control.
Thank you
A fairly simple approach (I hope) would be to try and convert this into some kind of graph problem.
Let's say every file is a node with edges between every two files.
The weight of an edge between two nodes would be, for instance, the number of different lines between the files (or some some other function).
What you do next is find a non-cyclic path that traverses all files with the minimum cost. something like this, if you know the first file and the last.
You could add an empty file and the latest version you have as your start and end nodes.
I'm guessing this won't give you the exact result, but it'll probably give you a good starting point.
Hope this is helpful.

To find all possible permutations of a given string

This is just to work out a problem which looks pretty interesting. I tried to think over it, but couldn't find the way to solve this, in efficient time. May be my concepts are still building up... anyways the question is as follows..
Wanted to find out all possible permutation of a given string....... Also, share if there could be any possible variations to this problem.
I found out a solution on net, that uses recursion.. but that doesn't satisfies as it looks bit erroneous.
the program is as follows:-
void permute(char s[], int d)
{
int i;
if(d == strlen(s))
printf("%s",s);
else
{
for(i=d;i<strlen(s);i++)
{
swap(s[d],s[i]);
permute(s,d+1);
swap(s[d],s[i]);
}
}
}
If this program looks good (it is giving error when i ran it), then please provide a small example to understand this, as i am still developing recursion concepts..
Any other efficient algorithm, if exists, can also be discussed....
And Please,, this is not a HW........
Thanks.............
The code looks correct, though you only have the core of the algorithm, not a complete program. You'll have to provide the missing bits: headers, a main function, and a swap macro (you could make swap a function by calling it as swap(s, d, i)).
To understand the algorithm, it would be instructive to add some tracing output, say printf("permute(%s, %d)", s, d) at the beginning of the permute function, and run the program with a 3- or 4-character string.
The basic principle is that each recursive call to permute successively places each remaining element at position d; the element that was at position d is saved by putting it where the aforementioned remaining element was (i.e. the elements are swapped). For each placement, permute is called recursively to generate all desired substrings after the position d. So the top-level call (d=0) to permute successively tries all elements in position 0, second-level calls (d=1) try all elements in position 1 except for the one that's already in position 0, etc. The next-to-deepest calls (d=n-1) have a single element to try in the last position, and the deepest calls (d=n) print the resulting permutation.
The core algorithm requires Θ(n·n!) running time, which is the best possible since that's the size of the output. However this implementation is less efficient that it could be because it recomputes strlen(s) at every iteration, for a Θ(n²·n!) running time; the simple fix of precomputing the length would yield Θ(n·n!). The implementation requires Θ(n) memory, which is the best possible since that's the size of the input.
For an explanation of the recursion see Gilles answer.
Your code has some problems. First it will be hard to implement the required swap as a function in C, since C lacks the concept of call by reference. You could try to do this with a macro, but then you'd either have to use the exclusive-or trick to swap values in place, or use a temporary variable.
Then your repeated use of strlen on every recursion level blows up your complexity of the program. As you give it this is done at every iteration of every recursion level. Since your string even changes (because of the swaps) the compiler wouldn't even be able to notice that this is always the same. So he wouldn't be able to optimize anything. Searching for the terminating '\0' in your string would dominate all other instructions by far if you implement it like that.

Resources