Reduce graph by removing transitive nodes - algorithm

I'm working on a problem that requires removing transitive nodes in a graph. More specifically I need to reduce the number of edges by removing the nodes from the path between two sets of nodes.
Picture says a thousand words so here is what I'm trying to do
The graph contains 3 types of nodes (Ai, Bi, Ci). I'd like to reduce the graph by removing all the nodes Bi on a path between nodes Ai and Ci, whilst preserving reachability between the Ai,Ci nodes.
This is a tripartite graph, indeed, and I'm wondering if there is an efficient algorithm that can reduce it as per the description shown in the attached picture.

If we let A denote the adjacency matrix between the As and Bs and B denote the adjacency matrix between the Bs and Cs, then the adjacency matrix of the resulting graph is the Boolean matrix product AB. In theory the fast matrix multiplication algorithms apply (if you have dense matrices), but in practice I doubt that they'll help much.

Related

faster graph traversal algorithms compared to dfs

I have an undirected unweighted graph represented using adjacency matrix where each node of the graph represents a space partition (e.g. State) while the edges represent the neiborhood relationship (i.e. neighboring states sharing common boundaries). My baseline algorithm uses DFS to traverse the graph and form subgraphs after each step (i.e. adding the new node visited which would result in a bunch of contiguous states). With that subgraph I perform a statistical significance test on certain patterns which exist in the nodes of the graph (i.e. within the states).
At this point I am essentially trying to make the traversal step faster.
I was wondering if you all could suggest any algorithm or resources (e.g. research paper) which performs graph traversal computationally faster than DFS.
Thanks for your suggestion and your time!
Most graph algorithms contain "for given vertex u, list all its neighbors v" as a primitive. Not sure, but sounds like you might want to speed up this piece. Indeed, each country has only few neighbors, typically much less than the total number of countries. If this is the case, replace adjacency matrix graph representation with adjacency lists.
Note that the algorithm itself (DFS or other) will likely remain the same, with just a few changes where it uses this primitive.

Is there any other Data structure to represent Graph other than Adjacency List or Adjacency Matrix?

I was looking for different Data structures for representing Graph and I came accross Nvidia CUDA Toolkit and found out new way to represent graph with the help of source_indices, destination_offsets.
Fascinated by this innovative representation of graph, I searched out for other ways of representing Graphs. But not found anything new.
I was wondering if there was any other way to represent Graph other than Adjacency Matrix or Lists...
I was wondering if there was any other way to represent Graph other
than Adjacency Matrix or Lists...
There are alternatives to the adjacency list or the adjacency matrix, such as edge list, adjacency map or forward star to name a few. Given this graph (images taken from here):
this is the adjacency matrix representation:
this is the adjacency list representation:
this would be another alternative, the edge list:
and another pretty common one is the forward star representation:
If you get into this research field you will find a good number of approaches, mainly optimizations for specific cases, taking into account factors such as:
Graph size (number of nodes)
Density of the graph
Directed or undirected graph
Static or dynamic graph
Graph known at compile time or constructed at runtime
Node IDs (labeled sequentially or not)
...
These optimizations can, for example, support reordering of the nodes in a preprocessing stage to increase reference locality. There is a lot of work for shortest path algorithms, specially when calculating the shortest path in a world map.
One example of optimization would be a dynamic graph structure (Packed-Memory Graph (PMG)) which is suited for large-scale transportation networks.
There is another representation of graphs using Adjacency Set. It is very much similar to adjacency list but instead of using Linked lists, Disjoint Sets [Union-Find] are used. You can read about disjoint sets ADT here.
If E is the number of edges and V is the number of vertices in the graph, then Adjacency set representation of graph takes up (E+V) space.
Complexities of other operations while using adjacency set representation:
Checking edge between vertex v and w : log(Degree(v))
Iterate over edges incident to vertex v: Degree(v)

partitioning tree nodes with clustering algorithm

I have an undirected tree structure where each edge has length associated. Given a random selection of nodes, I need to partition them into subsets S1, S2, ..., Sk (all may be equal or close in size) such that the path length between any two nodes within a subset is <= those in two different subsets.
My understanding is that this is not a standard graph partitioning problem.
To me it seemed closest to k-means algorithm, but that does not take arbitrary distance measures.
Googling around, I found that k-medoid algorithm allows arbitrary distance metric, which looks promising. I am thinking of creating pairwise distance matrix (D[i,j] = path length from node i to node j) and putting that into a k-medoid algorithm. Is that a reasonable approach or are there standard algorithms that I am missing?

Maximal number of vertex pairs in undirected not weighted graph

Given undirected not weighted graph with any type of connectivity, i.e. it can contain from 1 to several components with or without single nodes, each node can have 0 to many connections, cycles are allowed (but no loops from node to itself).
I need to find the maximal amount of vertex pairs assuming that each vertex can be used only once, ex. if graph has nodes 1,2,3 and node 3 is connected to nodes 1 and 2, the answer is one (1-3 or 2-3).
I am thinking about the following approach:
Remove all single nodes.
Find the edge connected a node with minimal number of edges to node with maximal number of edges (if there are several - take any of them), count and remove this pair of nodes from graph.
Repeat step 2 while graph has connected nodes.
My questions are:
Does it provide maximal number of pairs for any case? I am
worrying about some extremes, like cycles connected with some
single or several paths, etc.
Is there any faster and correct algorithm?
I can use java or python, but pseudocode or just algo description is perfectly fine.
Your approach is not guaranteed to provide the maximum number of vertex pairs even in the case of a cycle-free graph. For example, in the following graph your approach is going to select the edge (B,C). After that unfortunate choice, there are no more vertex pairs to choose from, and therefore you'll end up with a solution of size 1. Clearly, the optimal solution contains two vertex pairs, and hence your approach is not optimal.
The problem you're trying to solve is the Maximum Matching Problem (not to be confused with the Maximal Matching Problem which is trivial to solve):
Find the largest subset of edges S such that no vertex is incident to more than one edge in S.
The Blossom Algorithm solves this problem in O(EV^2).
The way the algorithm works is not straightforward and it introduces nontrivial notions (like a contracted matching, forest expansions and blossoms) to establish the optimal matching. If you just want to use the algorithm without fully understanding its intricacies you can find ready-to-use implementations of it online (such as this Python implementation).

Algorithm to generate a network that fills a 10x10 grid with a source, horizontal lines, right angles, t-junctions and nodes?

Just for fun I'm trying to create a non-flash version of http://www.jurjans.lv/stuff/net/FreeNet.htm. It's all pretty straightforward stuff, but I'm mentally stuck on how to generate the initial network.
I could do it square by square with lots of if/else logic checking neighboring squares, but frankly it seems very laborious and I wonder if there's a much much MUCH smarter way. Generate a mathematical graph or something similar and then translate that into the grid?
I'm not asking for someone to code it all for me - just point me in the right direction!
The completed circuit appears to be a spanning tree.
There is an easy way to generate random minimum spanning trees by:
assigning random weights from some distribution to the edges of an undirected graph, and then constructing the minimum spanning tree of the graph.
So to summarise:
Build a graph with a vertex at the centre of each square
Add edges between each vertex and its neighbours above/down/left/right
Assign random weights (e.g. uniform real from 0 to 1) to each edge
Construct minimum spanning tree e.g. with Prim or Kruskal
Convert graph into tiles
If there are certain shapes you want to forbid (e.g. a fully connected vertex) you may want an extra iteration to increase the weight for edges used in any tiles that are illegal, and then regenerate the spanning tree until you end up with a legal graph.

Resources