### Setup and Run Python Chapter 10 Examples Source: https://github.com/jinghuazhao/dsa/blob/master/docs/python/README.md This snippet outlines the steps to set up a Python virtual environment, install necessary libraries (opencv-python, scikit-learn, keras, tensorflow), and run specific Python scripts for Chapter 10 examples. ```bash module load python/3.9.12/gcc/pdcqf4o5 python -m venv venv source venv/bin/activate pip install opencv-python pip install scikit-learn pip install keras pip install tensorflow python < iris.py python < nn_by_hand.py python < mnist.py ``` -------------------------------- ### Java Setup with algs4 Library Source: https://context7.com/jinghuazhao/dsa/llms.txt Instructions for downloading and using the algs4.jar library for Java projects. Includes running examples directly from the jar and building from source. ```bash # Download pre-built jars wget https://algs4.cs.princeton.edu/code/algs4.jar wget https://introcs.cs.princeton.edu/java/stdlib/stdlib.jar # Run FFT example directly from the jar java -cp .:algs4.jar:stdlib.jar edu.princeton.cs.algs4.FFT 2 # Build from source and run Critical Path Method (CPM) git clone https://github.com/kevin-wayne/algs4 cd algs4 && mvn package # produces target/algs4-1.0.0.0.jar cd src/main/java/ wget https://algs4.cs.princeton.edu/44sp/jobsPC.txt javac edu/princeton/cs/algs4/CPM.java java -cp .:../../../../../../stdlib.jar edu.princeton.cs.algs4.CPM < jobsPC.txt ``` -------------------------------- ### Java Setup with Bailey JavaStructures Source: https://context7.com/jinghuazhao/dsa/llms.txt Instructions for downloading and using Bailey's JavaStructures library. Includes compiling and running the Sort example. ```bash # Fetch library and example archives wget http://www.cs.williams.edu/JavaStructures/Software_files/bailey.jar wget http://www.cs.williams.edu/JavaStructures/Software_files/structure-examples.tgz tar fvzx structure-examples.tgz # Compile and run Sort — type numbers, end with Ctrl-D cp eg/structure5/Sort.java . javac Sort.java java -cp .:bailey.jar Sort # 5 3 8 1 ^D # Output: 1 3 5 8 ``` -------------------------------- ### Python Environment Setup for Deep Learning Source: https://context7.com/jinghuazhao/dsa/llms.txt Sets up a Python virtual environment and installs necessary libraries for deep learning examples. Includes commands for loading modules, creating/activating venv, and installing packages. ```python # Environment setup (HPC module system example) # module load python/3.9.12/gcc/pdcqf4o5 python -m venv venv source venv/bin/activate pip install opencv-python scikit-learn keras tensorflow # Run chapter examples python iris.py # iris dataset classification python nn_by_hand.py # manual backpropagation walkthrough python mnist.py # MNIST digit recognition with Keras # Expected output (iris.py excerpt): # Accuracy: 0.9733... ``` -------------------------------- ### Compile and Run Sort.java Example Source: https://github.com/jinghuazhao/dsa/blob/master/docs/java/README.md Extract example files, copy Sort.java, compile it, and run it using the bailey.jar library. This example sorts a list of numbers provided via console input. ```bash tar fvzx structure-examples.tgz cp eg/structure5/Sort.java . javac Sort.java java -cp .:bailey.jar Sort ``` -------------------------------- ### Run FFT Example with Algs4 Source: https://github.com/jinghuazhao/dsa/blob/master/docs/java/README.md Execute the FFT example from the algs4 library. This requires the algs4.jar and stdlib.jar to be in the classpath. ```bash java -cp .:algs4.jar:stdlib.jar edu.princeton.cs.algs4.FFT 2 ``` -------------------------------- ### Download and Compile Average.java Source: https://github.com/jinghuazhao/dsa/blob/master/docs/java/README.md Download the Average.java file, compile it, and run it. This is a basic example for input/output operations. ```bash wget https://introcs.cs.princeton.edu/java/15inout/Average.java javac Average.java java Average ``` -------------------------------- ### Compile and Run CPM Example Source: https://github.com/jinghuazhao/dsa/blob/master/docs/java/README.md Compile the CPM.java file and run it with input from jobsPC.txt. This requires the stdlib.jar to be in the classpath and the working directory to be set. ```bash export wd=$(PWD) javac edu/princeton/cs/algs4/CPM.java java -cp .:$wd/stdlib.jar edu.princeton.cs.algs4.CPM < jobsPC.txt ``` -------------------------------- ### Download Algs4 and Stdlib Jars Source: https://github.com/jinghuazhao/dsa/blob/master/docs/java/README.md Download the algs4.jar and stdlib.jar files, which are essential libraries for running Sedgewick and Wayne's Algorithms 4th Edition examples. ```bash wget https://algs4.cs.princeton.edu/code/algs4.jar wget https://introcs.cs.princeton.edu/java/stdlib/stdlib.jar ``` -------------------------------- ### Clone Algs4 Repository and Build Source: https://github.com/jinghuazhao/dsa/blob/master/docs/java/README.md Clone the algs4 GitHub repository, build the project using Maven, and navigate to the source directory. This setup is for examining the algorithm code. ```bash git clone https://github.com/kevin-wayne/algs4 cd algs4 mvn package cd src/main/java/ ``` -------------------------------- ### Download Java Structures Libraries Source: https://github.com/jinghuazhao/dsa/blob/master/docs/java/README.md Download necessary JAR files and source code archives for the Java Structures library. These are required for running examples from Bailey's book. ```bash wget http://www.cs.williams.edu/JavaStructures/Software_files/bailey.jar wget http://www.cs.williams.edu/JavaStructures/Software_files/structure-source.tgz wget http://www.cs.williams.edu/JavaStructures/Examples_files/structure-examples.tgz ``` -------------------------------- ### Binary Search Tree (BST) Implementation Source: https://context7.com/jinghuazhao/dsa/llms.txt A complete BST implementation in C with typed insert, find, and delete operations. Supports sequential and random key loading from the command line. ```c /* bst.c — binary search tree: insert / find / delete */ #include #include typedef enum { STATUS_OK, STATUS_MEM_EXHAUSTED, STATUS_DUPLICATE_KEY, STATUS_KEY_NOT_FOUND } statusEnum; typedef int keyType; typedef struct { int stuff; } recType; typedef struct nodeTag { struct nodeTag *left, *right, *parent; keyType key; recType rec; } nodeType; nodeType *root = NULL; statusEnum insert(keyType key, recType *rec) { /* ... full impl in bst.c */ } statusEnum find (keyType key, recType *rec) { /* ... full impl in bst.c */ } statusEnum delete(keyType key) { /* ... full impl in bst.c */ } /* Build and query a BST: gcc bst.c -o bst ./bst 1000 # insert/find/delete 1000 sequential keys ./bst 2000 r # insert/find/delete 2000 random keys Output: "seq bt, 1000 items" or "ran bt, 2000 items" — no error lines means all OK */ ``` -------------------------------- ### Quick Sort Implementation Source: https://context7.com/jinghuazhao/dsa/llms.txt Implements the in-place quicksort algorithm using a middle-element pivot strategy with recursive partitioning. Suitable for large datasets where average-case performance is critical. ```c /* quick.c — recursive quicksort */ #include #include void quick_sort(int array[], int first, int last) { int temp, low = first, high = last; int pivot = array[(first + last) / 2]; do { while (array[low] < pivot) low++; while (array[high] > pivot) high--; if (low <= high) { temp = array[low]; array[low++] = array[high]; array[high--] = temp; } } while (low <= high); if (first < high) quick_sort(array, first, high); if (low < last) quick_sort(array, low, last); } void main() { int values[20]; for (int i = 0; i < 20; i++) values[i] = rand() % 100; quick_sort(values, 0, 19); for (int i = 0; i < 20; i++) printf("%d ", values[i]); } /* gcc quick.c -o quick && ./quick Output: sorted 20 random integers in ascending order */ ``` -------------------------------- ### AVL Tree (Self-Balancing BST) Implementation Source: https://context7.com/jinghuazhao/dsa/llms.txt An AVL tree implementation in C with character keys. Insertions automatically trigger single or double rotations (LL, RR, LR, RL); deletion rebalances using Balance_Left_Heavy / Balance_Right_Heavy. ```c /* avl_tree.c — AVL insert and delete with rotations */ #include #include #define F 0 #define T 1 struct NODE { char Info; int Flag; struct NODE *Left_Child, *Right_Child; }; struct NODE *Binary_Tree(char Info, struct NODE *Parent, int *H); struct NODE *Delete_Element(struct NODE *Parent, char Info, int *H); void Output(struct NODE *Tree, int Level); /* Interactive session: gcc avl_tree.c -o avl && ./avl Input characters one at a time; tree is printed after each insertion. Inserting D, B, F, A, C, E, G produces a balanced tree: G F E D C B A Enter 'b' to stop inserting, then enter keys to delete with live rebalancing. */ ``` -------------------------------- ### Warshall's Algorithm for Transitive Closure (C) Source: https://context7.com/jinghuazhao/dsa/llms.txt Computes the transitive-closure (reachability) matrix of a directed graph using Warshall's algorithm. Requires a pre-defined size for adjacency matrices. ```c #include #define size 10 void Warshall(int a[][size], int p[][size], int n) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) p[i][j] = (a[i][j] != 0) ? 1 : 0; for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) p[i][j] = p[i][j] | (p[i][k] & p[k][j]); } /* gcc warshall.c -o warshall && ./warshall Input 4 vertices and adjacency matrix: 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 Path matrix output: 0 1 1 1 0 0 1 1 0 0 0 1 0 0 0 0 */ ``` -------------------------------- ### C Array-Based Stack Implementation Source: https://context7.com/jinghuazhao/dsa/llms.txt Implements a fixed-size integer stack using a global array. Includes push, pop, and display operations with overflow and underflow detection. The stack size is defined by the `size` macro. ```c /* stack_a.c — array-based stack with push/pop */ #include #define size 100 int top = -1; int stack[size]; void push(int s[], int d) { if (top == (size - 1)) { /* overflow */ return; } s[++top] = d; } int pop(int s[]) { if (top == -1) return 0; /* underflow */ return s[top--]; } void display(int s[]) { for (int i = top; i >= 0; i--) printf("%d\n", s[i]); } /* Interactive session: gcc stack_a.c -o stack_a && ./stack_a Push->i Pop->p Quit->q Input choice: i → enter 42 → stack: [42] Input choice: i → enter 17 → stack: [17, 42] Input choice: p → popped: 17, stack: [42] Input choice: q → exit */ ``` -------------------------------- ### Graph Dijkstra's Shortest Path Algorithm Source: https://context7.com/jinghuazhao/dsa/llms.txt Implements Dijkstra's algorithm using a permanent/temporary label scheme to find the shortest weighted path between two vertices in a directed graph. ```c /* dijkstra.c — shortest path between two vertices */ #include #define size 20 #define infinity 9999 int Short_Path(int a[size][size], int n, int s, int t, int path[size], int *dist); /* gcc dijkstra.c -o dijkstra && ./dijkstra Input number of vertices: 5 Input adjacency matrix (0 = no edge): 0 10 0 5 0 0 0 1 2 0 0 0 0 0 4 0 3 9 0 2 7 0 6 0 0 Input starting vertex: 1 Input destination: 5 Output: Shortest path: 1 => 4 => 5 Minimum distance = 7 */ ``` -------------------------------- ### Graph Breadth-First Search (BFS) Source: https://context7.com/jinghuazhao/dsa/llms.txt Performs BFS on a graph represented by an adjacency matrix. Computes path lengths from a source vertex and prints each vertex's distance and adjacency list. ```c /* bfs.c — BFS with path-length computation */ #include #define size 20 /* Build graph from adjacency matrix, run BFS from source vertex. gcc bfs.c -o bfs && ./bfs Input the number of vertices: 4 Input the adjacency matrix (row by row): 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 Input starting vertex (0-3): 0 Output: Vertex Length Connectivity A 0 B C B 1 A D C 1 A D D 2 B C */ ``` -------------------------------- ### Graph Depth-First Search (DFS) Source: https://context7.com/jinghuazhao/dsa/llms.txt Performs recursive DFS using an adjacency-matrix and adjacency-list representation. Records the DFS visit order as each vertex's path length. ```c /* dfs.c — recursive DFS visit-order numbering */ #include #define size 20 void DFS(int index, int *dist, struct Vertex vert[]) { vert[index].visit = 1; /* mark visited */ vert[index].path_length = (*dist)++; for (struct Edge *lnk = vert[index].Edge_Ptr; lnk; lnk = lnk->next) if (!vert[lnk->terminal].visit) DFS(lnk->terminal, dist, vert); } /* gcc dfs.c -o dfs && ./dfs Same adjacency-matrix input as BFS; output shows DFS discovery order: Vertex Length Connectivity A 0 B C B 1 A D D 2 B C C 3 A D */ ``` -------------------------------- ### C Singly Linked List Traversal Source: https://context7.com/jinghuazhao/dsa/llms.txt Demonstrates basic character linked list traversal. The list is manually created with four nodes. The code iterates through the list and prints the data of each node. ```c /* link.c — basic character linked list traversal */ #include #include struct list { char data; struct list *next; }; typedef struct list Linked; void main() { Linked *start, node1, node2, node3, node4; start = &node1; node1.data = 'A'; node1.next = &node2; node2.data = 'B'; node2.next = &node3; node3.data = 'C'; node3.next = &node4; node4.data = 'D'; node4.next = NULL; /* Traverse and print: A B C D */ for (int i = 0; i < 4; i++) { printf("%c\n", start->data); start = start->next; } } /* gcc link.c -o link && ./link Output: A B C D */ ``` -------------------------------- ### Recursive Euclidean GCD Source: https://context7.com/jinghuazhao/dsa/llms.txt Computes the Greatest Common Divisor (GCD) of two integers using the recursive Euclidean algorithm. Ensure inputs are non-negative. ```c /* gcd.c — recursive Euclidean GCD */ #include int Greatest_Comm_Div(int n, int m) { if ((n >= m) && ((n % m) == 0)) return m; return Greatest_Comm_Div(m, n % m); } void main() { printf("GCD(48, 18) = %d\n", Greatest_Comm_Div(48, 18)); printf("GCD(100, 75) = %d\n", Greatest_Comm_Div(100, 75)); } /* gcc gcd.c -o gcd && ./gcd GCD(48, 18) = 6 GCD(100, 75) = 25 */ ``` -------------------------------- ### Tower of Hanoi Recursive Solution Source: https://context7.com/jinghuazhao/dsa/llms.txt Solves the Tower of Hanoi puzzle recursively. Prints each disk move between three named pegs. ```c /* hanoi.c — Tower of Hanoi recursive solution */ #include void hanoi_tower(char peg1, char peg2, char peg3, int n) { if (n == 1) { printf("Move disk from %c to %c\n", peg1, peg3); return; } hanoi_tower(peg1, peg3, peg2, n - 1); hanoi_tower(peg1, peg2, peg3, 1); hanoi_tower(peg2, peg1, peg3, n - 1); } void main() { hanoi_tower('X', 'Y', 'Z', 3); } /* gcc hanoi.c -o hanoi && ./hanoi Output (7 moves for n=3): Move disk from X to Z Move disk from X to Y Move disk from Z to Y Move disk from X to Z Move disk from Y to X Move disk from Y to Z Move disk from X to Z */ ``` -------------------------------- ### Bubble Sort Implementation Source: https://context7.com/jinghuazhao/dsa/llms.txt Sorts an integer array in ascending order using the basic O(n^2) bubble sort algorithm. This implementation has a time complexity of O(n^2) in all cases. ```c /* bubble.c — bubble sort */ #include #include void bubble_sort(int array[], int size) { int temp; for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) if (array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } void main() { int values[10]; for (int i = 0; i < 10; i++) values[i] = rand() % 100; bubble_sort(values, 10); for (int i = 0; i < 10; i++) printf("%d ", values[i]); } /* gcc bubble.c -o bubble && ./bubble Output (ascending order): 7 11 24 35 45 52 68 73 86 92 */ ``` -------------------------------- ### C Recursive Fibonacci Generator Source: https://context7.com/jinghuazhao/dsa/llms.txt Computes Fibonacci numbers recursively. This implementation demonstrates a classic divide-and-conquer approach. Note that this recursive method can be inefficient for large values of n due to repeated calculations. ```c /* fibo.c — recursive Fibonacci generator */ #include long fibo_number(int n) { if (n == 1 || n == 0) return 0; if (n == 2) return 1; return fibo_number(n - 1) + fibo_number(n - 2); } void main() { int n = 8; /* generate first 8 Fibonacci numbers */ for (int i = 1; i <= n; i++) printf("%ld ", fibo_number(i)); } /* gcc fibo.c -o fibo && ./fibo Output: 0 1 1 2 3 5 8 13 */ ``` -------------------------------- ### C Matrix Multiplication Source: https://context7.com/jinghuazhao/dsa/llms.txt Implements matrix multiplication for two matrices. Ensure the number of columns in the first matrix equals the number of rows in the second matrix for valid multiplication. The program takes dimensions and values interactively. ```c /* mult_mat.c — multiply two matrices */ #include #include #define row 10 #define col 10 void mat_mult(float mat1[row][col], int row1, int col1, float mat2[row][col], int row2, int col2, float mat_res[row][col]) { int i, j, k; if (col1 == row2) { for (i = 0; i < row1; i++) for (j = 0; j < col2; j++) { mat_res[i][j] = 0; for (k = 0; k < col1; k++) mat_res[i][j] += mat1[i][k] * mat2[k][j]; } } else { printf("Multiplication is not possible\n"); exit(0); } } /* Usage: compile and run, then enter dimensions and values interactively. gcc mult_mat.c -o mult_mat && ./mult_mat Input the row of matrix 1: 2 Input the col of matrix 1: 3 Input the row of matrix 2: 3 Input the col of matrix 2: 2 Result: 2×2 matrix printed to stdout */ ``` -------------------------------- ### Merge Sort Implementation for Floats Source: https://context7.com/jinghuazhao/dsa/llms.txt Implements top-down recursive merge sort for float arrays. It splits subarrays via `merge_pass` and merges them using `merge_sort`. Note the fixed-size temporary array. ```c /* merge.c — recursive merge sort for float arrays */ #include #include void merge_sort(float l[], int top, int size, int bottom) { float temp[1000]; int f = top, s = size + 1, t = top; while (f <= size && s <= bottom) temp[t++] = (l[f] <= l[s]) ? l[f++] : l[s++]; while (f <= size) temp[t++] = l[f++]; while (s <= bottom) temp[t++] = l[s++]; for (int u = top; u <= bottom; u++) l[u] = temp[u]; } void merge_pass(float a[], int m, int n) { if (m != n) { int mid = (m + n) / 2; merge_pass(a, m, mid); merge_pass(a, mid + 1, n); merge_sort(a, m, mid, n); } } void main() { float list[10] = {64, 25, 12, 22, 11, 90, 3, 47, 55, 38}; merge_pass(list, 0, 9); for (int i = 0; i < 10; i++) printf("%.0f ", list[i]); } /* gcc merge.c -o merge && ./merge Output: 3 11 12 22 25 38 47 55 64 90 */ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.