0% found this document useful (0 votes)
3 views6 pages

23 Cs 01026

The document describes functions for creating and solving a crossword puzzle using an adjacency list representation. The CreateEdge and CreateGraph functions build the graph structure by establishing connections between intersecting words, while the isvalid function checks if a proposed solution adheres to the crossword constraints. The solveCrossword function employs backtracking to assign words to the puzzle slots, ensuring all constraints are satisfied, with a worst-case time complexity of O(w^n * n*L).

Uploaded by

shatadeep2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

23 Cs 01026

The document describes functions for creating and solving a crossword puzzle using an adjacency list representation. The CreateEdge and CreateGraph functions build the graph structure by establishing connections between intersecting words, while the isvalid function checks if a proposed solution adheres to the crossword constraints. The solveCrossword function employs backtracking to assign words to the puzzle slots, ensuring all constraints are satisfied, with a worst-case time complexity of O(w^n * n*L).

Uploaded by

shatadeep2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Task 2

1.​ CreateEdge Function

void CreateEdge(list *arr[], int v1, int v2, int w) {


list *newNode = new list;
newNode->data = v2;
newNode->length = w;
newNode->next = nullptr;

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.

If an intersection is found, it calls CreateEdge to establish a connection between the words in


the adjacency list, storing the intersection position.

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.

Each word scans its length: O(L).


Each word is checked against n other words for possible intersections: O(n).
Worst-case time complexity: O(n^2 * L).
Task 3

3. isvalid function:

bool isvalid(char sol[][100], list *arr[], int n) {


for (int i = 0; i < n; i++) {
list *temp = arr[i]->next;
while (temp != nullptr) {
int v2 = temp->data;
int pos1 = temp->length - 1;

list *temp2 = arr[v2]->next;


int pos2 = -1;
while (temp2 != nullptr) {
if (temp2->data == i) {
pos2 = temp2->length - 1;
break;
}
temp2 = temp2->next;
}

if (pos2 == -1 || sol[i][pos1] != sol[v2][pos2]) {


return false;
}
temp = temp->next;
}
}
return true;
}

Functionality: Check if a given solution satisfies all given crossword constraints


Logic:

1. Iterate over all words in the crossword matrix.


2. For each word, traverse its list to find its intersections.
3. Retrieve the intersection pos in both words.
4. Verify that the characters at the intersection pos match.
5. If any mismatch is found, return false . Otherwise, return true if all intersections are correct.

Time Complexity:​

Each word check in list : O(n)


Each list traversal is at most : O(L)
Worst time complexity : O(n*L)

4. solveCrossword function

bool solveCrossword(list *arr[], char wordList[][100], char sol[][100],


bool used[], int indx2, int n, int w) {
if (indx2 == n) {
return isvalid(sol, arr, n);
}

for (int i = 0; i < w; i++) {


if (used[i] || strlen(wordList[i]) != arr[indx2]->length) {
continue;
}

strcpy(sol[indx2], wordList[i]);
used[i] = true;

if (solveCrossword(arr, wordList, sol, used, indx2 + 1, n, w)) {


return 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy