Suggested Tools for Visualizing a Subgraph DAG - graph

I am writing a program which basically processes a subgraph DAG, i.e., the graph is a directed acyclic graph and each node of the graph is a subgraph. For example, for an edge A->B in the DAG, node A is a subgraph of a triangle and node B is a subgraph of a four clique. The edge A->B indicates an extension from a triangle to a four clique by adding one subgraph vertex and three subgraph edges.
+=======+ +=======+
| a---b | | a---b |
| | / | | |\ /| |
| | / | =====> | | X | |
| |/ | | |/ \| |
| c | | c---d |
+=======+ +=======+
A B
For many purposes, I want to visualize this subgraph DAG, i.e., the visualization can show the entire structure of the DAG and show the subgraph of all the nodes at the same time. Also it would be better if it can also show properties of vertices and edges.
I am able to list all the nodes and edges of the DAG and the subgraphs of all the nodes in any format. But what I don't know is what tool is the best for such visualization. I tried graphviz by exporting the subgraph DAG into dot format and use graphviz to convert the dot file to a png. The problem of graphviz is it can show the subgraph of nodes very well but it fails to show the structure of the DAG in a human-readable way. It would be good if
for a DAG edge A->B, node A is placed above node B. (graphviz now puts all the DAG nodes in the same row)
the nodes are placed properly such that the intersection among the DAG edges is minimized.
Are there any other alternatives? Thanks in advance!

If I understand your requirements, I think this dot program comes pretty close:
digraph dag {
compound=true // needed for A->B edge (kludge, but documented)
edge [dir=none] // no arrowheads
subgraph clusterA{
labelloc=b
label="A"
Aa -> Ab // [constraint=false] // keep on same rank
Ab -> Ac
Aa -> Ac
{
rank=same
Aa [label="a"]
Ab [label="b"]
}
Ac [label="c"]
}
subgraph clusterB{
labelloc=b
label="B"
Ba -> Bb // [constraint=false] // keep on same rank
Bc -> Bd // [constraint=false] // keep on same rank
Bb -> Bc
Ba -> Bc
Bb -> Bd
Ba -> Bd
{
rank=same
Ba [label="a"]
Bb [label="b"]
}
{
rank=same
Bc [label="c"]
Bd [label="d"]
}
}
Ac -> Ba [dir=forward ltail=clusterA lhead=clusterB minlen=2]
}
Here is the output:

Related

divide rectangle into n equal parts

i want to code a video platform and have a problem and can't think of a solution right now.
I want to divide a rectangle into equal parts
Came up with a solution for a square but i am unable to come up with a solution for different ratios.
Maybe u guys can help me.
example:
BAD GOOD
n=4
________ ________
| | | | | | | |
| | | | | |---|---|
|_|_|_|_| |___|___|
n=2
________ ________
| | | | |
| | | | |
| | | |–––––––|
| | | | |
|___|___| |_______|
Let X be the width of the rectangle and let Y be the height. Let the goal be to divide this rectangle into N rectangles of equal area whose sides are as close to equal as possible.
The solution is not difficult. First, find all factors of the N. Then, write N as a product of two numbers A and B such that A and B are as close as possible (that is, there is no other choice A' and B' such that |A' - B'| < |A - B|). Assume we chose A > B. All we have to do is put A - 1 lines along the long side of the rectangle, and B - 1 lines along the short side.
For example: n = 4, A = 2 and B = 2 is optimal, so you put A - 1 = 1 and B - 1 = 1 lines along each side of the rectangle (as in your GOOD column for n = 4).
For example: n = 21, A = 7 and B = 3 is optional, so you'd put 6 lines along the long edge of the rectangle and 2 lines along the short edge, equally spaced:
_ _ _ _ _ _ _
|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|
Of course, for prime values of N, you are not going to get a very nice solution, but then there is no nice solution in that case. In such cases - where A and B are very very different and the rectangle's dimensions are not similarly different - you might want to choose another solution that doesn't require all rectangles have the same side lengths. You could do better by allowing 2 or 3 kinds of rectangles, or more, into the solution, for instance.

SSAS OLAP create a dimension with dynamic attributes

It is possible to create a dimension in a OLAP cube where the attibutes of this dimension are dynamic?
For instance, today I can have the attibutes A | B | C | D and tomorrow I can have the attibutes A | B | C | D | E.
Thanks in advance!

efficiently calculating connected components in pyspark

I'm trying to find the connected components for friends in a city. My data is a list of edges with an attribute of city.
City | SRC | DEST
Houston Kyle -> Benny
Houston Benny -> Charles
Houston Charles -> Denny
Omaha Carol -> Brian
etc.
I know the connectedComponents function of pyspark's GraphX library will iterate over all the edges of a graph to find the connected components and I'd like to avoid that. How would I do so?
edit:
I thought I could do something like
select connected_components(*) from dataframe
groupby city
where connected_components generates a list of items.
Suppose your data is like this
import org.apache.spark._
import org.graphframes._
val l = List(("Houston","Kyle","Benny"),("Houston","Benny","charles"),
("Houston","Charles","Denny"),("Omaha","carol","Brian"),
("Omaha","Brian","Daniel"),("Omaha","Sara","Marry"))
var df = spark.createDataFrame(l).toDF("city","src","dst")
Create a list of cities for which you want to run connected components
cities = List("Houston","Omaha")
Now run a filter on city column for every city in cities list, then create an edge and vertex dataframes from the resulting dataframe. Create a graphframe from these edge and vertices dataframes and run connected components algorithm
val cities = List("Houston","Omaha")
for(city <- cities){
val edges = df.filter(df("city") === city).drop("city")
val vert = edges.select("src").union(edges.select("dst")).
distinct.select(col("src").alias("id"))
val g = GraphFrame(vert,edges)
val res = g.connectedComponents.run()
res.select("id", "component").orderBy("component").show()
}
Output
| id| component|
+-------+------------+
| Kyle|249108103168|
|charles|249108103168|
| Benny|249108103168|
|Charles|721554505728|
| Denny|721554505728|
+-------+------------+
+------+------------+
| id| component|
+------+------------+
| Marry|858993459200|
| Sara|858993459200|
| Brian|944892805120|
| carol|944892805120|
|Daniel|944892805120|
+------+------------+

Finding Faces in a Geometric Directed Graph

I'm confronted with a rather unusual problem dealing with an directed geometric graph. Imagine the graph being borders of countries. I'm looking for a way to find the faces. My graph consists of directed edges which may form cycles (but not necessarily).
What I am looking for are the left and right faces as well as the predecessors and successors of the left and right faces for each edge.
Each face should be constructed anti-clockwise, meaning that the left face of an edge is always inside and the right face is outside of the specific face.
At the end of the day the nodes of the faces are geographic coordinates (lat and lon).
This is the information I am looking for (beginning from LeftFace..)
+------+-------+-------+----------+-----------+---------------------+--------------------+
| Edge | NodeA | NodeB | LeftFace | RightFace | PredecessorLeftFace | SuccessorRightFace |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E1 | P1 | P2 | A | C | E5 | E2 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E2 | P2 | P3 | A | C | E1 | E6 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E3 | P3 | P4 | A | B | E2 | E8 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E4 | P4 | P5 | A | C | E3 | E5 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E5 | P5 | P1 | A | C | E4 | E1 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E6 | P3 | P6 | B | C | E3 | E7 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E7 | P6 | P7 | B | C | E6 | E8 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E8 | P7 | P4 | B | C | E7 | E4 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
For each directed edge add also opposite edge in a graph. Than, for each (directed) edge find face in that direction. That means, traverse face edges so that in every vertex choose leftmost neighboring edge, until path returns to starting vertex. To choose leftmost edge, 2D positions of vertices are needed.
Example of choosing leftmost edge: going from P3 to P4 (opposite of E3). In P4 there are two possibilities to continue path, P5 and P7. Now check angles on the left side of edges. P3-P4-P5 is ~90deg, and P3-P4-P7 is ~270deg. Angle P3-P4-P5 is smaller than P3-P4-P7, so next edge in path is E4, and next point in path is P5.
Algorithm:
For each directed edge add opposite edge
While there are edges in graph
Choose one directed edge
Find edges that enclose face (on left side) starting from that edge
Add face to list of faces
Remove face edges from graph

Find out the language generated, given a context-free grammar?

Should I manually apply the production rules to find out the language generated by this grammar? This is tedious, is there any trick/tip to speed up things?
G = {{S, B}, {a, b}, P, S}
P = {S -> aSa | aBa, B -> bB | b}
EDIT: I found Matajon's answer a good one, that is thinking about each language generated by non-terminal symbol and then combine them.
But I'm still stuck when I have to solve some complicated examples like this:
G = {{S, R, T}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, P, S}
P = {S -> A | AS | BR | CT,
R -> AR | BT | C | CS,
T -> AT | B | BS | CR,
A -> 0 | 3 | 6 | 9,
B -> 1 | 4 | 7,
C -> 2 | 5 | 8}
Crazy, isn't it? Taken from past exams (programming languages course).
I don't know any general trick, but usually it helps to think about the language generated from each non-terminal.
In your example language generated from B is obviously L(B) = {b}^+. Then you think about S rules, using the first rule, you can generate sentencial forms {a^n.S.a^n | n >= 1}. If you use second rule on these sentencial forms or on S alone you can generate sentencial forms {a^n.B.a^n | n >= 1}.
Rest is pretty easy, you combine these two things and get L(G) = {a^n.b^+.a^n | n >= 1}
By the way, in the definition of grammar terminals and nonterminals are sets, not tuples. And third component is production rules, not start symbol. So you should write G = {{S, B}, {a, b}, P, S}.
Edit
Actually, there is a way to solve your second example without much thinking just by following something like a cookbook. Because, language generated by your second context-free grammar is in fact regular.
When you substitute rules for A, B and C to first three rules, you get
P' = {S -> 0 | 3 | 6 | 9 | 0S | 3S | 6S | 9S | 1R | 4R | 7R | 2T | 5T | 8T
R -> 0R | 3R | 6R | 9R | 1T | 4T | 7T | 2 | 5 | 8 | 2S | 5S | 8S
T -> 0T | 3T | 6T | 9T | 1 | 4 | 7 | 1S | 4S | 7S | 2R | 5R | 8R}
And P' is regular grammar. Because of that, you can convert it to nondeterministic finite automaton (there is really simple way, look for it) and then convert resulting NFA to the regular expression (this is not so simple but if you follow an algorithm and don't get lost, you should be ok). And it from regular expression it is easy to tell what language it describes.
Also, once you have NFA for this language you can look at it and determine what it does logically (it has something to do with counts of 1,4,7 and 2,5,8 in the word and mod 3 of their difference. Think it through, it is your homework, afterall :-) )
Of course, if you don't context-free grammar generating regular language you can't use this trick. There is no general way to tell what language the grammar generates (language equality problem for CFG's is undecideable), you have to think about every single example and look for similarities and patterns in it's logical structure.
I think you'll just need to apply the production rules.

Resources