So I want to store all the actors from a production in a graph. So from the input we will read like this:
Let N
be the number of movies we read, for each movie we read: the name of the movie, on the next line would be the number of the actors (let that be nr
) now on the next nr
lines would be the name of each actor. In a movie every actor is "connected" with every other actor.
What I've done so far:
I built an binary search tree based on the actor's name, and in every "node" of this binary search tree I have stored: the id of actor, all the movies he played in and his/her name.
So it would be like this:
typedef struct binaryTree
{
int size;
int id;
int movieSize;
int movieCapacity;
char **movieName;
char *actorName;
struct binaryTree *left;
struct binaryTree *right;
} *BinaryTree;
Now using this binary search tree, I want to search for every movie, and for every actor in that movie I want to connect theirs ids togheter in the graph.
Now here is the problem: I made the BST, but now while searching for every movies name, I will have to search for it in every actor, so that would still be O(nr)
for every search, so that didn't really help me. What would be another way of thinking about this to make the initialisation of the graph easier and efficient?
EDIT:
For a better visualisation of the data:
Movie1: Movie2: Movie3: Movie5:
Actor1 Actor1 Actor2 Actor6
Actor5 Actor3 Actor4 Actor7
Actor3 Actor4 Actor5 Actor8
The actual input that will be read:
4
Movie1
3
Actor1
Actor5
Actor3
Movie2
3
Actor1
Actor3
Actor4
Movie3
3
Actor2
Actor4
Actor5
Movie4
3
Actor6
Actor7
Actor8
This would be read in the order of : Movie1 with Actor1, Actor5, Actor3, Movie2 with Actor1, Actor3, Actor4, etc. (as you can see when you read Actor1 the second time it won't be added a new node but it will add a new movie to Actor1's list of movies.
Also here is a photo of the binary tree based on this data:
I want my data structure to support fast access between id and the name of the actor and also fast sorting of the data. That is the reason why I thought a BST would be appropiate and from there to build the graph with the IDs.The question is how do I really proceed after making this binary search tree on every actor's name with build up the graph or is there another (better) way of storing/accomplishing this?
Here is a photo of the graph I want to build where every number of the node is the number that is inside the actor's name ( for simplification purposes):