You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable.
You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?.
Return the answers to all queries. If a single answer cannot be determined, return -1.0.
Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.
Note: The variables that do not occur in the list of equations are undefined, so the answer cannot be determined for them.
Input: equations = [[“a”,“b”],[“b”,“c”]], values = [2.0,3.0], queries = [[“a”,“c”],[“b”,“a”],[“a”,“e”],[“a”,“a”],[“x”,“x”]]
Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000]
Explanation:
Given: a / b = 2.0, b / c = 3.0
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
note: x is undefined => -1.0
Input: equations = [[“a”,“b”],[“b”,“c”],[“bc”,“cd”]], values = [1.5,2.5,5.0], queries = [[“a”,“c”],[“c”,“b”],[“bc”,“cd”],[“cd”,“bc”]]
Output: [3.75000,0.40000,5.00000,0.20000]
Input: equations = [[“a”,“b”]], values = [0.5], queries = [[“a”,“b”],[“b”,“a”],[“a”,“c”],[“x”,“y”]]
Output: [0.50000,2.00000,-1.00000,-1.00000]
From: LeetCode
Link: 399. Evaluate Division
1. Graph Construction:
2. Graph Representation:
3. Query Processing:
4. Detail Workflow:
5. DFS Detail:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct Node {
char* var;
double value;
struct Node* next;
} Node;
typedef struct Graph {
Node** nodes;
int size;
int maxSize;
} Graph;
Graph* createGraph(int size) {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->nodes = (Node**)calloc(size, sizeof(Node*));
graph->size = 0;
graph->maxSize = size;
return graph;
}
int getIndex(Graph* graph, char* var) {
for (int i = 0; i < graph->size; i++) {
if (graph->nodes[i] && strcmp(graph->nodes[i]->var, var) == 0) return i;
}
return -1;
}
void addEdge(Graph* graph, char* src, char* dest, double value) {
int srcIndex = getIndex(graph, src);
if (srcIndex == -1) {
srcIndex = graph->size++;
graph->nodes[srcIndex] = (Node*)malloc(sizeof(Node));
graph->nodes[srcIndex]->var = src;
graph->nodes[srcIndex]->value = 1.0;
graph->nodes[srcIndex]->next = NULL;
}
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->var = dest;
newNode->value = value;
newNode->next = graph->nodes[srcIndex]->next;
graph->nodes[srcIndex]->next = newNode;
}
double dfs(Graph* graph, char* src, char* dest, double product, int* visited) {
int index = getIndex(graph, src);
if (index == -1) return -1.0;
if (strcmp(src, dest) == 0) return product;
visited[index] = 1;
for (Node* node = graph->nodes[index]->next; node; node = node->next) {
int nextIndex = getIndex(graph, node->var);
if (nextIndex != -1 && !visited[nextIndex]) {
double result = dfs(graph, node->var, dest, product * node->value, visited);
if (result != -1.0) return result;
}
}
return -1.0;
}
double* calcEquation(char *** equations, int equationsSize, int* equationsColSize, double* values, int valuesSize, char *** queries, int queriesSize, int* queriesColSize, int* returnSize) {
Graph* graph = createGraph(equationsSize * 2);
for (int i = 0; i < equationsSize; i++) {
addEdge(graph, equations[i][0], equations[i][1], values[i]);
addEdge(graph, equations[i][1], equations[i][0], 1.0 / values[i]);
}
double* results = (double*)malloc(sizeof(double) * queriesSize);
*returnSize = queriesSize;
for (int i = 0; i < queriesSize; i++) {
int* visited = (int*)calloc(graph->size, sizeof(int));
results[i] = dfs(graph, queries[i][0], queries[i][1], 1.0, visited);
free(visited);
}
// Don't forget to free the graph memory here
return results;
}