I'm just starting out to learn the basics of graph theory, and my textbook is a little unclear about a simple concept. The term "adjacency" as far as I understand, given a undirected graph, if A and B nodes are connected, A is adjacent to B, and B is adjacent to A. I was wondering if this was still true given a directed graph where A points to B?
Thanks
It looks like it was pretty well explained, but to provide some visuals. Adjacent edges are two nodes that are connected, and there are two basic setups:
In an undirected graph, two nodes A and B connected by an edge are adjacent to each other
In a directed graph, two nodes A and B connected by an edge from A to B means that you can get to B from A (or, B is adjacent to A):
In a directed graph where A points to B, {A,B} would be included in the graph's adjacency list and {B,A} would not be. That is, A is adjacent to B, but not vice-versa.
In a digraph, there is an edge from v1 to v2, then v2 is adjacent to v1. (From v1 to v2 as in v2 is the head and v1 is the tail.)
In an undirected graph this is symmetric - if v2 is adjacent to v1 then v1 is also adjacent to v2, and we say v1 ~ v2.
In a digraph, v1 may not necessarily also be adjacent to v2, so we say v1 ↓ v2.
EDIT: also, you could try asking this sort of question on the CSTheory Stackexchange site in the future - you might get better answers.
Related
I am looking back at some of my algorithms homework(exam soon haha), and I am having troubles understanding the solution to one of the questions.
The Question
Tutor Solution
I am having difficulties visualizing the solution, I understand that if you have an odd number of cycles than your graph cannot be bipartite. But as I stated, I don't understand the shortest path from s to u and v to s.
Let's say G is bipartite. Then the vertex set can be divided into V1 and V2 s.t. every edge of G includes a vertex from each set. Then the vertices of every path in G alternate between V1 and V2, so the parity of the length of every path between every pair of vertices is the same. I.e., if G is bipartite and v,w are two vertices of G, then the length of every path connecting v & w is either even or odd.
If there is an edge (u,v) connecting two vertices in the same layer then this is violated. The BFS path to v and u have the same length, so same parity, since v & u are in the same layer. The edge between v & u gives us a path longer by 1, so of a different parity. Contradiction.
The tutor's solution isn't as clear as it could be, since it talks about cycles, and the two paths don't necessarily form a cycle since they can share vertices. And the definition of bipartite graph is slightly wrong (or uses non-standard definitions of edges, cross-product etc. where the things aren't pairs but 2-element sets).
But instead of the definition as given, I'd just say that a graph is bipartite if the vertices can be coloured black or white, such that each edge goes between different-coloured vertices. (Equivalently you can simply say "the graph is 2-colourable").
From this definition, it follows that if there's an even length path between two vertices they must be the same colour, otherwise different colours. In the BFS on a bipartite graph, a vertex u is layer i has a path of length i from s to u, so all vertices in the same layer have the same colour. Thus there can be no edge between two vertices in the same layer.
I am thinking an algorithm to solve the problem below:
A given graph composed of vertices and edges.
There are N customers who want to travel from a vertex to another vertex.
And each customer requirement need a directed edge to connect two vertices.
The problem is how to find the minimum number of edges to satisfy all customers requirements ?
There is a simple example:
Customer 1 wants to travel from vertex a to vertex b.
Customer 2 wants to travel from vertex b to vertex c.
Customer 3 wants to travel from vertex a to vertex c.
The simplest way is to give an edge for each customers:
edge 1: vertex a -> vertex b
edge 2: vertex b -> vertex c
edge 3: vertex a -> vertex c
But actually there only needs 2 edges (i.e. edge 1 and edge 2) to satisfy three customer requirements.
If the number customers is large, how to find the minimum edges to satisfy all customer requirements ?
Is there a algorithm to solve this problem ?
You can model the problem as a mixed integer program. You can define binary variables for "arc a-> b is used" and "customer c uses arc a -> b" and write down the requirements as linear inequalities. If your graph is not too large, you can solve such models in reasonable time by a mixed integer program solver (CPLEX, GUROBI, but there also free alternatives on the web).
I know that this solution requires some work if you are not familiar with linear programming, but it guarantees to find best solutions in finite time and you can probably solve it for (say) 1000 customers and 1000 arcs.
If you have N vertices, you can always construct a solution with N (directed) edges. Just create a directed cycle V_1 -> V_2 -> V_3 ->... -> V_N -> V_1. You can never have directed path from every vertex V_a to every other vertex V_b with fewer edges (because you'd have a directed tree which necessarily contains a leaf). The leaf is either un-reachable (if the edge goes from leaf out) or the leaf is a sink (can't connect to anything else) if the edge is ->leaf.
No need to use any new algorithm. You can use BFS/DFS algorithm.
Find if there exists any path between source and destination.
if !true
add a direct edge between source and destination
count++;
return count;
Here the key part is instead of loop through the graph we have to loop through newly added edges.
You can use Disjoint set data structure.
https://en.wikipedia.org/wiki/Disjoint-set_data_structure
while (num_edges--)
if root(vertex_a) != root(vertex_b)
count++
union(vertex_a,vertex_B)
If I think of the same problem for undirected edges, what we are looking for is the minimum spanning tree (MST) of the original graph (constructed of all edges). The brief explanation is that for each edge E (v1 -> v2) if there is a second path to v2 from v1, there exist a cycle, and for each existing cycle there is an edge we can omit.
For finding MST of a directed graph there is Chu–Liu/Edmonds' algorithm you can use.
Note that you are assigning a weight of 1 to all of your edges.
Let’s say you have a bipartite graph G = (V, U, E)
v1 is connected to u1, u2, and u3
v2 is connected to u2 and u3
v3 is connected to u3.
By looking at the graph, I know that the minimum vertex covers are {v1, v2, u3} and {v1, u2, u3}, but I’m not sure how to use the bipartite matching/vertex cover algorithm to find this out. Here
and Here
When I perform algorithm by hand, the output is just the trivial minimum vertex cover, all of V. Am I doing something wrong?
Edit:
The maximum matching for the graph are the edges (v1, u1), (v2, u2), and (v3, u3). Given the maximum matching, the next step is to start at an unsaturated vertex (a vertex that is not one of the endpoints of a matched edge)
But in this case all the vertices are saturated, so I don't know how to proceed.
Konig's theorem has two directions. The easy direction, corresponding to weak linear programming duality, is that the vertex cover is at least as large as the matching. This is because, in every vertex cover, each matching edge has at least one of its endpoints present.
The hard direction of Konig's theorem, corresponding to strong LP duality, is that there exists a vertex cover where at most (i.e., exactly) one endpoint of each matching edge is present. The thrust of Wikipedia's current proof is to use the matching to construct a vertex cover greedily, showing that, if this algorithm gets stuck, then the allegedly maximum matching has an augmenting path (a contradiction). Every edge is incident to a matched vertex, so the unmatched vertices can be excluded from the cover. Their neighbors in turn must be included. Their neighbors' neighbors can be excluded, etc.
You've noticed that this process sometimes fails to determine the status of each vertex. For this case, the Wikipedia editors write "If there is no such vertex, but there remain vertices not included in any previously-defined set Sk, arbitrarily choose one of these and let S2j+1 consist of that single vertex." One way to justify this step is as follows. Letting u be the chosen vertex and v be u's mate, we're pretending as though v's mate was not u but a newly created vertex u'. Then u is unmatched, so we reseed the algorithm by excluding u and cover the newly created edge u' -- v when we subsequently include v.
Given an undirected graph G=(V,E) , V1,V2 are subsets of V.
d(V1,V2)=min d(v1,v2) ,
so I need to figure out how to find d(V1,V2) in O(|V|+|E|)
if then d(V1,V2)=0
otherwise, I randomly pick v1' from V1 and run BFS(V1,v1'), save the furthest vertex from v1'
at v1''
I will do the same for some random vertex v2' from V2.
return d(V1,V2)=min{ d(v1',v2'), d(v1',v2''),d(v1''v2'),d(v1'',v2'')}
will that work? since runtime of BFS is O(|V|+|E|) the suggested algorithm will run in O(|V|+|E|)
IMO what you can do is as follows:
Scan the set V1 and note all edges that begin at nodes in V1 and end at nodes not in V1.
Now combine all the nodes in V1 into one node. The edges noted in step 1 shall be the edges going out from this node.
Do the same for V2.
Now it reduces to the shortest route problem between node V1 and V2. This can be solved by conducting a simple BFS on node V1 or V2 in O(E) where E is the number of edges in the graph.
I'm currently working on an implementation of the D*Lite algorithm from Sven Koenig.
http://idm-lab.org/bib/abstracts/papers/aaai02b.pdf. Basically I'm trying to understand all the details before starting to implement it. It seems that the algorithm works on directed graphs, that's the way to define the Pred and Succ functions.
How do I define the direction of the graphs and which the parameters decide the direction of the graphs. Should I use the value of some parameter like the g cost (which doesn't seem to be a good choice...since is the g cost along with the rhs value the one the algorithm updates) or the heuristic estimate of the distance?
D*, and D*-lite will both work on both directed and undirected graphs.
A graph is G = (V, E), where V is a list of configurations (or states) that can be reached. E is a list of the connections between vertices. In a directed graph, E is a set of edges which are ordered pairs (u, v), where both u and v are vertices. In an undirected graph, E is a set of unordered pairs.
Planning on an undirected graph is equivalent to planning on a directed graph, with a bidirectional edge. That is, if (u,v) is an edge (v, u) will also be an edge.
How you construct the graphs is application specific, and varies from simple grids to much more complex strategies like lattice approximations to forward kinematics.