General Flashcards
What is a graph?
A graph is a non linear data structure consisted of vertices and edges
What are the 4 types of graphs?
Undirected graph, Directed graph, Weighed graph and Unweighed graph.
What is a vertex in a graph?
A vertex is an element from the graph, a node.
What is an edge?
An edge is a pair of connected vertices (v1,v2).
What is an undirected graph?
An undirected graph is a graph where the edges between vertices has no direction and have a two way relationship. Ex: doubly linked list.
What is a directed graph
A directed graph is graph where the edges have a specific direction and have a one way relationship. Ex: single linked list.
What is a weighed graph?
A weighed graph is a graph where the edges have weight.
This weight can be represented by any type of value like time, distance, quantity, etc.
In a map, the weight between two vertices, or the edge weight, would be the distance.
What is a path?
A path is the sequence of vertices to go through between one vertex and the other. From vertex “a” to vertex “n” there can be multiple paths between two vertices.
What is path length in a graph?
The number of vertices in a determined path.
What is a graph cycle?
A graph cycle is a path that starts and ends on the same vertex.
What is a negative weight cycle?
In a graph if the sum of all the weights in a cycle is negative it’s called a negative weight cycle.
What is graph connectivity?
If there is at least one path between two vertices, it means those vertices are connected.
What is a vertex degree?
The term degree applies to unweighed graphs. The degree of a vertex is the number of edges connecting the vertex.
If A connects to B, D and C it means that A has the edges (A,B), (A,D) and (A,C) hence A vertex degree is 3.
Does a vertex on a weighed graph has degrees?
No. Degrees only apply to unweighed graphs.
What is indegree in a vertex from a directed graph?
In a directed graph indegree is the number of edges coming TO the vertex.
If there are two edges (A, B) and (D, B) and the direction is from left to right, then B indegree is 2 because there are two edges coming TO vertex B from other vertices
What is outdegree in a vertex from a directed graph?
In a directed graph outdegree is the number of edges coming FROM the vertex.
If there are two edges (A, B) and (A, D) and the direction is read from left to right, then A outdegree is 2 because there are two edges coming FROM vertex A to other vertices.
What is a disjoint set?
Disjoint sets (also known as union-find) is a data structure where two or more sets don't have any element in common (intersection). Ex: [1,2,3] [4,5] are disjoint sets because they don't have any intersection.
In graph this can be translated by isolated paths that don’t have any element in common.
Ex: Edge (1,2) and Edge (2,5) vs Edge (3,4) and Edge (4,6)
What is a common use for a disjoint sect data structure?
A disjoint sect data structure can be used to identify the relationship between two vertices (connected or not).
E.g. in a social media whether two people are connected or not at any level.
What are the two main functions from a DisjointSet?
Union(v1,v2) and Find(v1)
How the union function in a disjoint set works?
Union will connect two vertices with each other.
The simplest representation uses an array where the index is the vertex and the value is the parent vertex (in case of a numeric representation).
How the find function in a disjoint set works?
The find function will find the ROOT for a given vertex.
If two vertices have the same root it means they are connected.
How the quick find implementation works on a disjoint set?
The union(v1,v2) function will store the root nodes instead of parent nodes. To do that it needs to replace all vertices values that reference rootY with rootX (selected head). eg: rootX and rootY, replace all vertices with rootY value by rootX.
The find(v) function will return the root which is the value on the vertex index.
union(v1,v2) time complexity O(n)
find(v) time complexity O(1)
How quick union implementation works on a disjoint set?
The union(v1, v2) function will store the parent vertex for each vertex instead of the root vertex.
The find(v) function will keep searching the parent until the vertex and parent are the same.
union(v1,v2) time complexity 0(n) (based on the find function)
find(v) time complexity O(n) (with n being the height of the tree)
What is more efficient quick find or quick union in a disjoint set?
Quick union is more efficient because only on the worst case the union function will be O(n).
On quick find the union function will always be O(n) no matter what since it needs to go over the whole array every time you call union method.
Is there a way to make quick union function faster in a disjoint set?
Yes there is. It’s using union by rank.
What is union by rank function in a disjoint set?
Union by rank optimizes quick union by balancing the tree by rank with the rank being the height of the tree.
The root from the smaller rank (height) is joined into the root from highest rank (height).
e.g:
rank[rootX] > rank[rootY] : array[rootY] = rootX;
rank[rootX] < rank[rootY] : array[rootX] = rootY;
rank[rootX] == rank[rootY] : array[rootX] = rootY; rank[rootX]++;
find(v) time complexity O(logN)
union(v1,v2) time complexity O(logN)
TLDR;
Instead of selecting rootX or rootY for all operations, we find the bigger height and do the union with the root from the smallest height into the root from the bigger height. If the height is the same select any of them and increase the height of the selected by 1;
public void union(int x, int y) { int rootX = find(x); int rootY = find(y); if (rootX != rootY) { if (rank[rootX] > rank[rootY]) { root[rootY] = rootX; } else if (rank[rootX] < rank[rootY]) { root[rootX] = rootY; } else { root[rootY] = rootX; rank[rootX] += 1; } } }
What is path compression strategy in a disjoint set?
Path compression is used to optimize the find function so after finding the root we update the parent of all children to be the root vertex. (recursion)
Next time we try to find the root again on any vertex from that path it will be pointing directly to root instead of their immediate parent.
public int find(int x) { if (x == root[x]) { return x; } return root[x] = find(root[x]); }