23 Cs 01026
23 Cs 01026
if (arr[v1] == nullptr) {
arr[v1] = newNode;
} else {
list *temp = arr[v1];
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}
Functionality:
The CreateEdge function builds the adjacency list representation of the crossword
puzzle. It connects intersecting words by creating edges based on their respective
positions.
Logic:
1. The CreateEdge function establishes an edge between two vertices (v1 and
v2) in an adjacency list representation.
2. A new node (newNode) of type list is dynamically allocated.
3. The data field of newNode is set to v2, indicating the destination vertex.
4. The length field stores the weight of the edge.
5. The next pointer of newNode is initialized to nullptr.
6. If arr[v1] is nullptr, it implies no existing edges from v1, so newNode is
assigned to arr[v1].
7. Otherwise, a pointer temp iterates through the linked list to find the last node.
8. The next pointer of the last node is updated to point to newNode, adding the
edge at the end of the list.
2. CreateGraph Function
void CreateGraph(list *arr[], int **matrix, int row, int col, int
*x1, int *y1, int *x2, int *y2, bool axis[], int n) {
for (int k = 0; k < n; k++) {
int i = x1[k], j = y1[k];
if (matrix[i][j] == 0) {
cout << "Invalid input" << endl;
return;
}
if (axis[k]) {
int len = 0;
for (int temp = y1[k]; temp <= y2[k]; temp++) {
len++;
for (int v = 0; v < n; v++) {
if (!axis[v] && y1[v] == temp && x1[v] <= i &&
x2[v] >= i) {
CreateEdge(arr, k, v, len);
}
}
}
} else {
int len = 0;
for (int temp = x1[k]; temp <= x2[k]; temp++) {
len++;
for (int h = 0; h < n; h++) {
if (axis[h] && x1[h] == temp && y1[h] <= j &&
y2[h] >= j) {
CreateEdge(arr, k, h, len);
}
}
}
}
}
}
Functionality: The CreateGraph function builds an adjacency list representation of the
crossword grid, where each node corresponds to a word, and edges denote intersections
between words.
Logic:
The CreateGraph function constructs an adjacency list representation of the crossword grid by
iterating through the given words.
It verifies if the starting position of each word is valid by checking matrix[i][j]. If invalid, it
prints an error message and exits the function.
If a word is placed horizontally (axis[k] == true), it iterates through its length from y1[k] to
y2[k], maintaining a length counter.
For each horizontal word, it checks for intersections with vertical words (!axis[v]) at the same
column position and within the row range of the vertical word.
Similarly, if a word is placed vertically (axis[k] == false), it iterates from x1[k] to x2[k],
checking for intersections with horizontal words.
If an intersection is found, CreateEdge is called to link the intersecting words in the adjacency
list.
Time Complexity:
Let n be the number of words in the crossword puzzle and L be the average word length.
3. isvalid function:
Time Complexity:
4. solveCrossword function
strcpy(sol[indx2], wordList[i]);
used[i] = true;
used[i] = false;
}
return false;
}
Functionality: This function backtracks to assign words to the crossword slots while ensuring all
constraints using isvalid function
Logic:
1. If all words have been placed (index == n), verify the solution's validity using
isvalid.
2. Otherwise, loop through the word list and check for words that match the required length
and are unused. Assign a suitable word.
3. Recursively invoke solveCrossword for the next index. If the recursive attempt fails,
backtrack and try a different word.
4. If no valid solution is found after testing all words, return false.
Time Complexity:
Let assume there are w words in the word list and L is avg word length
There are O(w^n) possible word assignments due to backtracking.
The validation check runs in O(n*L)
Worst case complexity : O(w^n * n*L), making it exponential.