Implementing BFS on same level vertexes of a graph data structure - graph

I'm trying to understand graphs in data structures and got struck here in understanding. can anyone help in understanding this approach. Graphs allow multiple vertexes to connect to any arbitrary vertex without any constraints. while inserting edges, it is possible the vertex to be connected may be on the same level or at any below levels.
In the above figure, BFS through graphs gives 5,6,7 in one and 5,7,6 in another. There is no constraint to get the same level vertexes on graph. How is this identified?
Please let me know as none of the resources point that differenece. Both 6,7 are unvisited from 5,if one refers to 7 and adds to queue(image-1). BFS will be violated.
EDIT:
In the above BFS Example image we have 5 vertexes and if we start from 5 we can either traverse to 6,7 as adjacent nodes. if we are trying to implement BFS here then we can either add 6 or 7 to queue but 7 is the valid one as it is at the same level. How is this identified?

The two drawings you posted show identical graphs: the same three vertices are connected with the same three edges. Graphs don't have levels. They don't keep track of any type of edge ordering, nor the horizontal or vertical layout of the vertices when the graph is drawn.
A BFS starting from vertex 5 could visit either 6 or 7 next. Either order is a valid BFS traversal.

Related

Graph traversal with a minimum number of "steps" (steps based on edge parameters)

There is a bidirectional graph. The graph edges have parameters (let it be a color). A step is a transition from one vertex to another. If at a certain moment the already visited vertices have unvisited edges with the same parameters, then in 1 step it is possible to go through all the edges with the same parameters. Edges can have multiple parameters.
I need to find a traversal algorithm to visit all the vertices with a minimum number of "steps".
Example graph (1 and 2 are starting (visited) vertices):
graph example
Expected result: 6, 3 and 5, 4 and 7 (3 steps)
I want to iterate over all graph traversal options to find the best. But maybe there is any better algorithm for this task? If not, what is the best way to find all traversal options?

Finding all possible directed graphs given a number of vertices

Is it possible to find all possible directed graphs given a pair of vertices and the information that an edge exists between them? For example if we know the vertices with edge pairs like
1 2
2 3
1 3
The possible directed graphs will be:
1→2, 2→3, 1→3
1→2, 2→3, 3→1
1→2, 3→2, 1→3
1→2, 3→2, 3→1
2→1, 2→3, 1→3
2→1, 2→3, 3→1
2→1, 3→2, 1→3
2→1, 3→2, 3→1
What data-structure to be used here to work with? What can be the working logic?
I was thinking of using adjacency matrix data structure and compute all possible adjacency matrix. Each adjacency matrix will represent a graph. We can use the graph as and when needed for tasks like checking whether cycle is present or not etc.
Apologies that this is more of a discussion than a programming question, but any help will be appreciated
You could maintain one undirected graph data structure G and work with the knowledge that the existence of an edge (u,v) means that there is only one directed edge in a particular instance of digraph possibility D.
If you want to maintain all possible digraphs separately, you would need 2^m many of them, where m is the number of edges. If the vertices and edges are always the same and only the direction is the invariant, then you could maintain 2^m bit-strings, each of length m. Each bit has a 0 or 1 depending on whether the edge (u,v) it corresponds to is u-->v or v<--u. Use the bit string to give direction to the undirected graph suggested above. You just need to generate all 2^m bit strings. Not very efficient... but you can't do better if you need to work with all possibilities.
You could use the bit string to construct a digraph. But, it would be more memory efficient at least to maintain only one bit-string per 'graph' rather than repeating the entire graph data structure with only directional changes. Record bit strings in a hash table: use each edge as a key and then bit value 0/1 depending on direction. Any graph traversal of one of the many possible digraphs D works on undirected G. Then in constant time you can check for incident (undirected) edges of a vertex, which are outgoing/incoming in D. So traversals can occur just as quickly by maintaining only 1 graph object and 1 bit hash table of size 2^m (rather than 2^m graph objects).

Convert a map with houses into a graph

I am curious how map software (Google/Bing maps) convert a map into a graph in the backend.
Now if we add houses between intersections 1 and 2, then how would the graph change. How do map software keep track of where the houses are?
Do they index the intersection nodes and also have smaller "subnodes" (between 1 and 2 in this case)? Or do they do this by having multiple layers? So when a user enters a home number, it looks up where the home is (i.e. between which vertices the home is located). After that, they simply apply a shortest path algorithm between those two node and at the beginning and the end, they basically make the home node go to one of the main vertices.
Could someone please give me a detailed explanation of how this works? Ultimately I would like to understand how the shortest path is determined given two the "address" of two "homes" (or "subnodes").
I can only speak for GraphHopper, not for the closed source services you mentioned ;)
GraphHopper has nodes (junctions) and edges (connection between those junctions), nearly exactly how your sketch looks like. This is very fast for the routing algorithms as it avoids massive traversal overhead of subnodes. E.g. in an early version we used subnodes everytime the connection was not straight (e.g. curved street) and this was 8 times slower and so we avoided those 'pillar' nodes and only used the 'tower' nodes for routing.
Still you have to deal with two problems:
How to deal with queries starting on the edge at e.g. house number 1? This is solved via introducing virtual nodes for every query (which can contain multiple locations), and you also need the additional virtual edges and hide some real edges. In GraphHopper we create a lightweight wrapper graph around the original graph (called QueryGraph) which handles all this. It then behaves exactly like a normal 'Graph' for every 'RoutingAlgorithm' like Dijkstra or A*. Also it becomes a bit hairy when you have multiple query locations on one edge, e.g. for a route with multiple via points. But I hope you get the main idea. Another idea would be to do the routing for two sources and two targets but initialized with the actual distance not with 0 like it is normally done for the first nodes. But this makes the routing algorithms more complex I guess.
And as already stated, most of the connections between junctions are not straight and you'll have to store this geometry somewhere and use it to draw the route but also to 'snap a location to the closest road' do finally do the actual routing. See LocationIndexTree for code.
Regarding the directed graphs. GraphHopper stores the graph via undirected edges, to handle oneways it stores the access properties for every edge and for every vehicle separately. So we avoid storing two directed edges and all of its properties (name/geometry/..), and make the use case possible "oneway for car and twoway for bike" etc. It additionally allows to traverse an edge in the reverse direction which is important for some algorithms and e.g. the bidirectional Dijkstra. This would not be possible if the graph would be used to model the access property.
Regarding 'nearly exactly how your sketch looks like': node 1, 3, 7 and 8 would not exist as they are 'pillar' nodes. Instead they would only 'exist' in the geometry of the edge.
To represent the connectivity of a road network, you want your directed road segments to be the graph nodes and your intersections to be collections of directed edges. There is a directed edge from X to Y if you can drive along X and then turn onto or continue on Y.
Consider the following example.
a====b====c
|
| <--one way street, down
|
d
An example connectivity graph for this picture follows.
Nodes
ab
ba
bc
cb
bd
Edges
ab -> bc
ab -> bd
cb -> ba
cb -> bd
Note that this encodes the following information:
No U-turns are allowed at the intersection,
because the edges ab -> ba and cb -> bc are omitted.
When coming from the right a left turn onto the vertical road is allowed,
because the edge cb -> bd is included.
With this representation, each node (directed road segment) has as an attribute all of the addresses along its span, each marked at some distance along the directed road segment.

Sequence of number of vertices in a graph

I want to generate a sequence of the number of vertices in all graphs which each edge has the same number of leaving edges. I dont have to generate the whole sequence. Let's say the first 50 if exists.
I want:
Input: the number of edges leaving each vertex
Output: a sequence of the number of vertices
So far, I have looked at complete graphs. Complete graphs with n vertices always have n-1 edges leaving each vertex. But there are other kinds of graphs that have this property. For example, some polyhedrons, such as snub dodecahedron and truncated icosidodecahedron have this property.
How should I approach my problem?
I think you mean regular graphs:
http://en.wikipedia.org/wiki/Regular_graph
http://mathworld.wolfram.com/RegularGraph.html
I made a regular graph generator which isn't flawless by the way:
once you generate the nodes, say from 1 to n. You want regularity r.
For node 1, make connections to the following nodes until you reach degree r for node 1.
For node 2 you already have degree 1 (because of node 1), you connect again to further nodes until you reach degree r for node 2 too. And this way till the last node.
The flaw is that you can't define an r-regular graph for any number of nodes. The algorithm mentioned doesn't detect that, so some errors may occur. Also, this isn't a random r-regular graph generator, but instead give one possible solution.
I'm not much of an explainer, so please ask if the description lacks somewhere.

How can you describe this kind of graph?

I know for sure that this is a simple directed graph. But I cannot say that this is a ring graph/network, because node 3 has a degree of 4. But as I imagine this, you cannot go to node 7 from node 3 if the preceding node is node 2, and you cannot go to node 4 from node 3 if the preceding node is node 6. That means the only way to traverse this graph is to start from one node and then go to the adjacent node that has a number greater than the current node (except for node 7 to node 1). What kind of graph is this? Thanks in advance!
http://img231.imageshack.us/img231/5492/graphl.jpg
Yes, it is a simple directed graph. It is also an Eulerian graph with exactly one Eulerian circuit. That's probably its most interesting property.
You could redraw the graph as a standard Eulerian Graph without the addition rules by dividing node 3 into two nodes, node 3 and node 6A, where 2-3-4 and 6-6A-7 are the only valid paths through the two nodes. At this point the graph would look like a figure eight. The nodes could then be rearranged in a circle while maintaining the topology.

Resources