Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data structure to represent many to many relationship

If we have Student and Course entity and the relationship between them is many to many i.e a Student can take many courses and a course can be taken by many students. If we have to represent this relationship what is the best data structure through which we can represent this relationship. If we use hashmap with student as the key and list of courses that student took as the value then we need another hashmap through which we can represent course to student relationship. Are there any best ways to represent this relationship so that searching is fast.

like image 984
Vaishu13 Avatar asked Jul 19 '15 07:07

Vaishu13


People also ask

In which type of data structure the elements have a one-to-many relationship?

In a relational database, a one-to-many relationship exists when one row in table A may be linked with many rows in table B, but one row in table B is linked to only one row in table A.

What is plex data structure?

A Plex data structure, is like a linked list of n-sized arrays. Where each array is called as Bead and each bead can contain either information or a pointer to another bead. It is a more efficient data structure alternative used to represent an "intricate network of interrelated parts".

What is relationship in data structure?

relation: A data structure representing Relations on Sets. A library to model relationships between two objects that are subclasses of Ord. We use a two Maps that allows fast searching either by the key element or the value element.

Which data structure can have more than one logical way of traversing?

Explanation: The answer is a, i.e., stack.


2 Answers

I think it's appropriate to use a combination of data structures. Here is a small example:

public class ManyToManyMap<S, C> {
    private Map<S, Set<C>> firstToSecondMap = new HashMap<>();

    private Map<C, Set<S>> secondToFirstMap = new HashMap<>();

    public void put(S first, C second) {
        if (!firstToSecondMap.containsKey(first)) {
            firstToSecondMap.put(first, new HashSet<>());
        }
        firstToSecondMap.get(first).add(second);

        if (!secondToFirstMap.containsKey(second)) {
            secondToFirstMap.put(second, new HashSet<>());
        }
        secondToFirstMap.get(second).add(first);
    }

    public Set<C> getFirst(S first) {
        return firstToSecondMap.get(first);
    }

    public Set<S> getSecond(C second) {
        return secondToFirstMap.get(second);
    }

    public Set<C> removeByFirst(S first) {
        Set<C> itemsToRemove = firstToSecondMap.remove(first);
        for (C item : itemsToRemove) {
            secondToFirstMap.get(item).remove(first);
        }

        return itemsToRemove;
    }

    public Set<S> removeBySecond(C second) {
        Set<S> itemsToRemove = secondToFirstMap.remove(second);
        for (S item : itemsToRemove) {
            firstToSecondMap.get(item).remove(second);
        }

        return itemsToRemove;
    }
}

And here is an example usage:

ManyToManyMap<String, String> mmMap = new ManyToManyMap<>();

mmMap.put("Tom", "Math");
mmMap.put("Tom", "Java");
mmMap.put("Tom", "Java");
mmMap.put("Mary", "Java");

Set<String> coursesByStudent = mmMap.getFirst("Tom"); // Java, Math
Set<String> studentByCourse = mmMap.getSecond("Java"); // Tom, Mary

mmMap.removeByFirst("Tom");
studentByCourse = mmMap.getSecond("Java"); // Mary
like image 114
dim Avatar answered Sep 20 '22 02:09

dim


A bi-directional graph can be used to implements many-to-many relationship where each node can be connected to many other nodes

like image 42
Dhruv Singh Avatar answered Sep 20 '22 02:09

Dhruv Singh