I have citie's location, I treat the cities as vertex. I want to created
undirected graph to later to calculate shortest path tree. My question is
how to create undirected graph for the cities first?
Graph consists of vertices and edges. So your task is to gather information about edges somehow.
If you don't have information about which cities are directly connected to which neighboring cities by plain road (with no cities in between), then you might use some heuristic assumption. For example, you could safely assume that each city has no more than N (let's say N=50) roads going out of it and then just fill your graph with edges that represent roads to N closest neighboring cities.
To find those, you could take all cities surrounding a certain city, for example in radius of M miles (let's say M=100), and out of those pick no more than N closest ones. Those city pairs would represent roads in your graph.
Generally graphs are created using Adjacency Matrix or Adjacency List, whether they are weighed or non-weighed.
Adjacency matrix is a simple 2D array edge[n][n] where n are the number of vertices and edge[a][b] = 1 or weight represents a connection between a & b nodes.
edge[a][b] = 0 means no connection. Adjacency Matrix is always symmetric for undirected graphs.
Depending on the type of application you may want to switch between Adjacency Matrix or Adjacency List. The former has less time complexity in graph operations but requires more space compared to the later.
You can find more about Adjacency Matrix here
Related
If the graph was unweighted you would just go to the row/column (in degree/out degree) of that vertex and count the 1s. But what about a weighted graph? My professor said just count all the non-zero edges but to my understanding it is possible for an edge to have a weight of zero, correct?
So in short: Given an adjacency matrix of a weighted graph with weights of zero allowed, how can you count the degree of a certain vertex?
It doesn't really make sense to have a zero-weight edge in a weighted graph. That depends on what your weights mean for the system you are modelling, of course.
You could have a graph where some have weights and some do not, potentially. In which case, you can't record this as an adjacency matrix since you can't distinguish between 'no edge' and 'unweighted edge'.
If you really needed to have a graph where some edges have no weight encoded in a matrix, then I guess you could do some kind of simple trick like add 1 to all weights when storing in the matrix, then subtract one when you want to calculate properties over those weights.
Assume a state has 10 cities A,B,C,D,E,F,G,H,I,J. Now let's say D and G both have an airport. Given the distance of each city from one another, what is the minimum length of road that should be built so that all the cities are connected to an airport? There can be a direct route or indirect route (i.e. via some other city) from each cities to an airport; our objective is to construct minimum length of road.
Your problem reduces to the minimum spanning tree problem by simply chaining together the vertexes associated to airports with edges of zero weight. As such, you can solve it by using e.g. the classical Prim algorithm:
Initialize the solution with all the vertexes containing an airport
Among all the remaining edges, select the cheapest one that augments the spanning tree
Iterate until every vertex is covered.
My question is similar to an existing one: https://math.stackexchange.com/questions/1691013/how-does-one-join-two-graphs-in-graph-theory
But I'm specifically wondering what would be the new edge's costs, if the two graphs already have some traversal cost associated to them?
Specification: Construct a directed graph where the nodes are labeled. The edges of the graph are labeled with a traversal cost. They also obey the usual triangle inequality. That is, in a triangle such as the triangle inequality x + y ≥ z where x, y, and z are the costs associated with traversing these edges. The costs are real numbers may not exceed some specified cost interval. At a minimum, your library should be able to add edges to a graph, join two graphs that operate on the same cost interval and disjoint sets of nodes, and compute whether there is a path from one node to another and, if so, its cost.
I understood all the other requirements of the specification except for joining two graphs where the edges have traversal costs.
My understanding: While joining the two graphs, I should still maintain the triangle equality but the new edge cost will be a value in this range: [min cost that satisfies triangle in equality, cost interval].
I'm doing this project as for my practice and got the specification from an internet source.
Comment if you like to know any more information.
I have a very large (10M+ edges, ~5M vertexes) bipartite undirected users-items graph in format
item1: user1, user2, user3, ...
or
userX1: itemY1
userX2: itemY2
...
I need to convert my graph into the items-items graph where weight of edge between i and j vertexes is equal to the number of users using both items simultaneously (i.e number of elements in the intersection of sets of vertexes adjacent to item_i and item_j). And here is the problem it seems it would require me to do $O(n^2)$ operations, where $n$ is the number of edges in graph which would be impossible on simple home pc i current own. Is there any solution to this? Some probabilistic data structure will suit my needs just fine because I'm allowed to lose some small percentage of data.
Notation: m = number of edges in the old graph
Sort the edge list E (version two of your proposed formats) by users (takes O(m log(m))
Go through E and determine all the runs of consecutive edges with same user (O(m))
Each pair of edges within a run gives you an edge in the new graph -> Add it to the edge list F of the new graph (O(|F| = n_user x max_n_items_per_user^2))
Contract all duplicates in F into single edges with the edge weights given by the number of duplicates (O(|F|))
If your graph is sparse, i.e. max_n_items_per_user is small, the above should be fairly efficient. Otherwise, this algorithm has the problem that it enumerates all edges in the new graph before contracting them, so one should think about how to get the edge weight directly.
Lets say I have a set of points and I have lines/edges between them. All of these edges create non-overlapping triangles within the convex hull of my points. All points are connected to triangles.
How can I efficiently check which points are part of which triangle? I could check incident points of each edge and gradually construct a triple of points, but that sounds awefully slow (o(n^2)?).
Is there something like linesweep or so to do that?
cheers.
If you have a 2-dimensional set-up like you described, then you have a fully triangulated planar graph (no intersecting edges when you exclude the endpoints) which spans the convex hull of your points. In this case, if you sort the edges around each vertex circularly according to the angle they make with the vertex, then you know for sure that each pair of adjacent edges makes a triangle. Furthermore, every triangle can be found this way if you perform this procedure for each vertex. Each triangle will be found 3 times when you iterate over all vertices. You can either use a hash table to detect duplicates, or sort all your triangles when you are done to identify duplicates. If you use hash table, then the overall complexity if you have V vertices is O(V log d), where d is the maximum degree of a vertex (because the total number of edges is linear in the number of vertices because you have a planar graph). So absolute worst-case is O(V log V), which is the same worst-case if you sort all triangles to find duplicates (because the max number of triangles is also linear in the number of vertices). The only caveat to make this work is that you need to know the neighbor vertices (i.e. the incidental edges) for each vertex.
The edges define an undirected graph G and the triangles are the set of cycles in G with length=3.
Geometric triangulations typically have relatively low nodal degree (degree d is the number of edges adjacent to each node, d<=10 is typical for geometric triangulations) and, as such, here is a reasonably efficient O(n*d^3) algorithm that can be used to construct the set of triangles.
Setup a graph-like data structure, supporting access to the list of edges adjacent to each node.
Iterate over all nodes. Consider all pairs of edges adjacent to a given node i. For a given pair of edges adjacent to i, we have a potential nodal triplet i,j,k. This triplet is a triangle if there is an edge joining nodes j,k, which can be checked by scanning the edge lists of j,k.
Duplicate triangles will be generated by a naive implementation of (2). Maintain a hash table of triangles to reject duplicate triplets as they're considered.
I've assumed that the edges define a valid disjoint triangulation, being non-intersecting, etc.
Hope this helps.