Resolving class inheritance algorithm - algorithm

I have a list of pairs with class inheritance information like this
[
[Person, null],
[Person, SpecialPerson], // Person extends SpecialPerson
[SpecialPerson, VerySpecialPerson], // SpecialPerson extends VerySpecialPerson
]
Is there any particular algorithm to flatten this information?
Like this:
Person -> SpecialPerson -> VerySpecialPerson

In the end, it boils down to a DAG (directed acyclic graph). Therefore you would do a breadth-first search or depth-first search. You only need the simplified case for trees.
Example (BFS, pseudo-code, untested):
List<Array<Typespec>> flatten(Array<Pair<Typespec,Typespec>> input) {
List<Array<Typespec>> result;
Queue<Array<Typespec>*> q;
var elem=&result.append([null]);
q.append(elem);
while (!q.empty()) {
for (i in input) {
if (i.first==q.front().back()) {
var elem=&result.append(q.front().clone().append(i.second));
q.append(elem);
}
}
q.pop_front();
}
return result;
}
This assumes that you meant [null,Person], instead of the other way round. Note that it produces a null at the start of every result, differing from your example.

Related

How to implement a Tree data structure with multiple roots

I need to implement a Tree data structure which should have multiple roots, not just 1 root. Look at this scenario, suppose I have to implement Tree data structure for "Book contents". Which are "Chapters > Sections > Sub-Sections" etc. The major problem is: There are multiple roots here, chapter 1, chapter 2, chapter 3 and so on. The root node must definitely start from chapters, since the type of content and functions are same starting from those level.
What my Task requires:
Tree with multiple roots
The Nodes are Ordered on horizontal level among same parent
It is a non-binary tree, meaning there can be any number of roots and any number of childs.
I have come with a solution, but I think it is a messy approach. I made one class like one would normally do for tree data structure. This class is "SimpleTree" which works for a single chapter as root node. To make multiple root nodes possible, I made another class "TopWrapperForSimpleTree". This top wrapper class has an Array in order to store multiple "SimpleTree" elements to it (Basically multiple roots). The messy part here is that I have to copy each function of "SimpleTree" and define it for the wrapper class as well. For example, a "Traversal Function" would traverse all the elements in the "SimpleTree". But now I have to implement a "Traversal Function" for "TopWrapperForSimpleTree" class as well where it would have to loop through all the Roots calling Traversal function on each of them and concatenating the result. The same goes for other functions like, finding a node, deleting a node etc.
To sum it all, I need a Tree Data structure which can have multiple roots. It should be ordered as well. The order is very important.
Image showing Tree with multiple roots
A "tree with multiple roots" is not a tree. When you consider each chapter to be a tree, then the collection of chapters is a forest. But you could just add a root node. Chapters belong to a Book, and the Book is then the root node.
You don't need a data structure for multiple roots. You need a data structure where nodes are multi-functional and can represent a book, a chapter, a section, ...etc without having to duplicate code. OOP is perfect for that (inheritance).
The idea is to define a class that has all the common features that all objects have in common. For instance, a book, a chapter, a section, ... all have a name, and they all can have "children". Iteration of a tree could be implemented as a method of this class.
Then a book would be an extension of this base class: a book can for instance have an author property. A section would also be an extension of the base class, but could have as extra property a page number. A chapter could be an extension of a section, as it also has a page number, but may in addition have a chapter number, ...etc.
Here is one of the many ways to do that. I use JavaScript here, but it works in a similar way in other OOP languages:
class Node {
constructor(name) {
this.name = name;
this.children = [];
}
add(...children) {
this.children.push(...children);
return this; // Return the instance, so method calls can be chained...
}
toString() {
return this.name;
}
* iter(depth=0) {
// A pre-order iteration through the whole tree that this node represents
yield [depth, this];
for (let child of this.children) {
yield * child.iter(depth+1);
}
}
* iterWithout(depth=0) {
// A pre-order iteration through the whole tree that this node represents
// ...but excluding the node on which the original call is made:
for (let child of this.children) {
yield [depth, child];
yield * child.iterWithout(depth+1);
}
}
}
class Book extends Node {
constructor(name, author) {
super(name);
this.author = author; // specific property for Book instance
}
toString() {
return super.toString() + ", by " + this.author;
}
}
class Section extends Node {
constructor(name, page) {
super(name);
this.page = page; // specific property for any section (also chapter)
}
toString() {
return super.toString() + ", page " + this.page;
}
}
class Chapter extends Section {
constructor(id, name, page) {
super(name, page);
this.id = id; // specific property for Chapter instance
}
toString() {
return "Chapter " + this.id + ". " + super.toString();
}
}
// Illustration of how it could be used:
function main() { // Demo
let book = new Book("The Perfect Theory", "Pedro G. Ferreira").add(
new Chapter(1, "If a Person Falls Freely", 1).add(
new Section("The Autumn of 1907", 1),
new Section("The Article in the Yearbook", 4),
new Section("Isaac Newton", 6),
new Section("Gravity", 9),
),
new Chapter(2, "The Most Valuable Discovery", 12),
new Chapter(3, "Correct Mathematics, Abominable Physics", 28),
new Chapter(4, "Collapsing Stars", 47)
);
for (let [depth, item] of book.iterWithout()) {
console.log(" ".repeat(depth) + item.toString());
}
}
main();

How to find depth of tree

I have class that can have nested same class instances:
class SomeClass
{
List<SomeClass> SomeClasses;
}
And... I need to know what is the most high amount in chain Parent - Children.
It's like that:
I so confused about this, so I don't even know how to start doing it. Most likely it can't be done without recursion.
Given that any SomeClass instance forms a tree, perhaps you are looking for the depth of the tree. If that's the case, you would indeed use recursion: write a function that accepts a SomeClass and returns 1 plus the maximum of the list formed by applying itself to the SomeClasses list.
Here for example is an implementation in JS:
const depth = ({ things }) => 1 + Math.max(0, ...things.map(depth))
depth({
things: [
{
things: [
{
things: []
},
{
things: [
{
things: []
}
]
}
]
}
]
});
// 4

Complex conditional filter design

I'm stuck at implementing some conditional rules in a form in the backend. Basically i need to come up with an efficient and scalable way of doing this. I was looking into binary trees and decision trees for this one but still not sure what's the best way to implement this.
As you can see there's one statement with the possibility of more than one conditions separated by logical AND/OR. Basically what i need to know is the data structure to store this information in the database. It will act as a filter when a form is submitted by the user based on the form values when it goes through the filter some action will take place as a result.
Your question is a bit generic, but let me see if I can help you get started. In Java, you can set up a class structure as follows :
interface ConditionTree {
boolean evaluate(...);
}
class OperatorNode implements ConditionTree {
List<ConditionTree> subTrees = ....;
#Override
boolean evaluate(...) {
if(operator == AND) {
//loop through and evaluate each sub tree and make sure they are all true,
//or return false
}
else if(operator == OR) {
//loop through and evaluate each sub tree, and make sure at least one is
//true, or return false
}
}
}
class LeafNode implements ConditionTree {
#Override
boolean evaluate(...) {
//get the LHS, operator, and RHS, and evaluate them
}
}
Notice it's an N-ary tree, not a binary tree.

remove cycle for a BGL graph

I want to remove cycles in a BGL graph (BGL adjacency_list) , and I found some people have suggested to use depth_first_search method that accepts a visitor like this
class CycleTerminator : public boost::dfs_visitor<> {
template <class Edge, class Graph>
void back_edge(Edge e, Graph& g) {
//implement
}
};
but I doubt whether I can use depth_first_search to remove edges in a graph, because depth_first_search has a const graph parameter, so it can not remove edges, I am looking forward to get an answer. Thank you very much.

String set implementation

I have to implement a set ADT for a pair of strings. The interface I want is (in Java):
public interface StringSet {
void add(String a, String b);
boolean contains(String a, String b);
void remove(String a, String b);
}
The data access pattern has the following properties:
The contains operation is far more frequent that the add and remove ones.
More often that not, contains returns true i.e. the search is successful
A simple implementation I can think of is to use a two-level hashtable, i.e. HashMap<String, HashMap<String, Boolean>>. But this datastructure makes no use of the two peculiarities of the access pattern. I am wondering if there is something more efficient than the hashtable, maybe by leveraging the access pattern peculiarities.
Personally, I would design this in terms of a standard Set<> interface:
public class StringPair {
public StringPair(String a, String b) {
a_ = a;
b_ = b;
hash_ = (a_ + b_).hashCode();
}
public boolean equals(StringPair pair) {
return (a_.equals(pair.a_) && b_.equals(pair.b_));
}
#Override
public boolean equals(Object obj) {
if (obj instanceof StringPair) {
return equals((StringPair) obj);
}
return false;
}
#Override
public int hashCode() {
return hash_;
}
private String a_;
private String b_;
private int hash_;
}
public class StringSetImpl implements StringSet {
public StringSetImpl(SetFactory factory) {
pair_set_ = factory.createSet<StringPair>();
}
// ...
private Set<StringPair> pair_set_ = null;
}
Then you could leave it up to the user of StringSetImpl to use the preferred Set type. If you are attempting to optimize access, though, it's hard to do better than a HashSet<> (at least with respect to runtime complexity), given that access is O(1), whereas tree-based sets have O(log N) access times.
That contains() usually returns true may make it worth considering a Bloom filter, although this would require that some number of false positives for contains() are allowed (don't know if that is the case).
Edit
To avoid the extra allocation, you can do something like this, which is similar to your two-level approach, except using a set rather than a map for the second level:
public class StringSetImpl implements StringSet {
public StringSetImpl() {
elements_ = new HashMap<String, Set<String>>();
}
public boolean contains(String a, String b) {
if (!elements_.containsKey(a)) {
return false;
}
Set<String> set = elements_.get(a);
if (set == null) {
return false;
}
return set.contains(b);
}
public void add(String a, String b) {
if (!elements_.containsKey(a) || elements_.get(a) == null) {
elements_.put(a, new HashSet<String>());
}
elements_.get(a).add(b);
}
public void remove(String a, String b) {
if (!elements_.containsKey(a)) {
return;
}
HashSet<String> set = elements_.get(a);
if (set == null) {
elements_.remove(a);
return a;
}
set.remove(b);
if (set.empty()) {
elements_.remove(a);
}
}
private Map<String, Set<String>> elements_ = null;
}
Since it's 4:20 AM where I'm located, the above is definitely not my best work (too tired to refresh myself on the treatment of null by these different collections types), but it sketches the approach.
Do not use normal trees (most standard library data structures) for this. There is one simple assumption, which will hurt you in this case:
The normal O(log(n)) calculation of operations on trees assume that comparisons are in O(1). This is true for integers and most other keys, but not for strings. In case of strings each comparison is on O(k) where k is the length of the string. This makes all operations dependent on the length, which will most likely hurt you if you need to be fast and is easily overlooked.
Especially if you most often return true there will be k comparisons for each string at each level, so with this access pattern you will experience the full drawback of strings in trees.
Your access pattern is easily handled by a Trie. Testing if a string is contained is in O(k) worst case (not average case as in a hash map). Adding a string is is also in O(k). Since you are storing two strings I would suggest, you don't index your trie by characters, but rather by some larger type, so you can add two special index values. One value for the end of the first string, and one value for the end of both strings.
In your case using these two extra symbols would also allow for simple removal: Just delete the final node containing the end symbol and your string will not be found anymore. You will waste some memory, because you still have the strings in your structure that have been deleted. In case this is a problem you could keep track of the number of deleted strings and rebuild your trie in case this get's to bad.
P.s. A trie can be thought of as a combination of a tree and several hashtables, so this gives you the best of both data structures.
I'd second the approach of Michael Aaron Safyan to use a StringPair type. Perhaps with a more specific name, or as a generic tuple type: Tuple<A,B> instantiated to Tuple<String,String>. But I would strongly suggest to use one of the provided set implementations, either a HashSet or a TreeSet.
Red-Black Tree implementation of the set would be a good option. C++ STL is implemented in Red-Black Tree

Resources