I have undirected, unweighed graph. I have a file that contains list of pairs (connected nodes). Each node may have any number of neighbours. I have to find one or all possible ways to traverse from a specified node to another.
I tried the depth first search - it works pretty well but my friends suggest that it may be pretty slowly. What other algorithms would you suggest? Could you please provide example pseudo code for them?
Related
With this undirected graph
In this graph I have different nodes with following types [A,B,C,D,E].
It means that is possible to exists different nodes with same type
Now imagine you have a set of node types [A,B,E]. You don´t know which node are those given nodes in the graph, only thing you know is the type of each node.
What you have to do is to find a best fit for that given set of nodes.
Node has to be connected to each other
I´ve been testing an algorithm which consists in steps below:
Convert the graph to a linked list
Generate all possible combinations between all nodes considering those given types and how many times a node type appears. The given example is [A,B,E] but it could be other set such as [A,B,C,A].
Some of possible (not all) combinations for [A,B,E] are:
Check if nodes in those combinations are connected to each other
the best fit is the first combination where all nodes are connected.
The problem is number of nodes in the given graph. For small sets of nodes and small graphs the algorthim is ok. But when the number of nodes are increased I have a thousands of possible combinations and those combinations consume a lot of memory.
I`ve been searching for some algorithm which could be able to solve this problem efficiently with low cost memory.
I have spent days reading and testing all kind of algorithm and until now I couldn`t find a better solution.
Suggestions are very appreciated
This is called the Graph Motif problem and unfortunately it's NP-hard, even when the graph is a tree with maximum degree 3: see Theorem 1 in https://people.mpi-inf.mpg.de/~hermelin/Conference%20Publications/Connected%20Motifs.pdf
This means it's very unlikely that any polynomial-time algorithm exists that can solve this problem.
I am working on a project for shortest path finding. I have looked at alot of resources online to come up with a good algorithm.
I am working with openstreetmap data and it's clear to me that I have to use A* algorithm.
While looking for different solutions, I have found that because a way is made of different nodes, one can prune away the intermediate nodes that are not junctions.
How can I do this in a programming language? If anyone has an idea or a further article that can help me, that would be really grateful.
The exact information I found about this pruning that's relevant to osm is this
parse all ways a second time; a way will normally become one edge,
but if any nodes apart from the first and the last have a link counter
greater than one, then split the way into two edges at that point.
Nodes with a link counter of one and which are neither first nor last
can be thrown away unless you need to compute the length of the edge.
Have a look into the GraphHopper project (where I'm the author of) or other routing projects for OSM already doing this. The idea is to count the number of ways one node is member of and mark nodes as junctions if they have a count of three or more (or just one if an endstanding 'junction').
Still the nodes in-between should be accessible as you need to plot the route for the end results after calculating the route. In GraphHopper we call them pillar nodes (nodes between junctions) and tower nodes (junctions). Here is more detailed information.
Another problem is that you have to calculate GPS precise routes and not just routes from junction to junction. Look into this change how we fixed this via virtual nodes and edges.
I've learned how these algorithms work, but what are they used for?
Do we use them to:
find a certain node in a graph or
to find a shortest path or
to find a cycle in a graph
?
Both of them just visit all the nodes and mark them visited, and I don't see the point of doing that.
I am sort of lost here what I am learning.
BFS and DFS are graph search algorithms that can be used for a variety of different purposes.
One common application of the two search techniques is to identify all nodes that are reachable from a given starting node. For example, suppose that you have a collection of computers that each are networked to a handful of other computers. By running a BFS or DFS from a given node, you will discover all other computers in the network that the original computer is capable of directly or indirectly talking to. These are the computers that come back marked.
BFS specifically can be used to find the shortest path between two nodes in an unweighted graph. Suppose, for example, that you want to send a packet from one computer in a network to another, and that the computers aren't directly connected to one another. Along what route should you send the packet to get it to arrive at the destination as quickly as possible? If you run a BFS and at each iteration have each node store a pointer to its "parent" node, you will end up finding route from the start node to each other node in the graph that minimizes the number of links that have to be traversed to reach the destination computer.
DFS is often used as a subroutine in more complex algorithms. For example, Tarjan's algorithm for computing strongly-connected components is based on depth-first search. Many optimizing compiler techniques run a DFS over an appropriately-constructed graph in order to determine in which order to apply a specific series of operations. Depth-first search can also be used in maze generation: by taking a grid of nodes and linking each node to its neighbors, you can construct a graph representing a grid. Running a random depth-first search over this graph then produces a maze that has exactly one solution.
This is by no means an exhaustive list. These algorithms have all sorts of applications, and as you start to explore more advanced algorithms you will often find yourself relying on DFS and BFS as building blocks. It's similar to sorting - sorting by itself isn't all that interesting, but being able to sort a list of values is enormously useful as a subroutine in more complex algorithms.
Hope this helps!
I'm looking for a quick method/algorithm for finding which nodes in a graph is critical.
For example, in this graph:
Node number 2 and 5 are critical.
My current method is to try removing one non-endpoint node from the graph at a time and then check if the entire network can be reached from all other nodes. This method is obvious not very efficient.
What are a better way?
See biconnected components. Calling them articulation points instead of critical nodes seems to yield better search results.
In any case, the algorithm consists of a simple depth first search where you maintain certain information for each node.
there are several better ways. research is always helpful
but since this is homework, the point of the exercise is likely to be to figure it out yourself
hint: how could you decorate the graph to tell you what nodes depend on what other nodes, and would this information perhaps be useful to spot the critical nodes?
I am given a network defined by nodes and links. I have to search all loops in the network. No coordinates will be given for the nodes.
Is there any existing algorithm or library that can do this. Or can you please give me some idea how I can approach this problem? I am programming in .NET.
I draw a diagram to illustrate what I need here
Try Distance vector Routing.
This algorithm finds the shortest path to all other nodes in a network from a node.
On the assumption that your edges are not directed and that there is a maximum of one edge between nodes then a http://en.wikipedia.org/wiki/Spanning_tree Depth-first spanning tree will cover all nodes and indicate where the cycles (which is what I think you mean by loops) will occur. We use this algorithm for finding "rings" in chemical structures. There are many implementations in many languages - here's a tutorial with an applet (http://oneweb.utc.edu/~Christopher-Mawata/petersen2/lesson20.htm)
The loops are called cycles, and this answer has a lot of informations for you.