Exploring Time Complexity of Kruskal’s Algorithm in Python

This article is a complementary resource to the DSA with Python course.

Exploring Time Complexity of Kruskal’s Algorithm in Python

A nested loop is a loop inside another loop. This is useful for working with multi-dimensional structures, grids, and patterns.

Example: Looping Through Two Lists

Imagine you have two lists: one for car attributes and one for car brands. We can use nested loops to print all possible combinations.


// List of car attributes
let attributes = ["Electric", "Fast"];

let cars = ["Tesla", "Porsche"];

for (let attribute of attributes) {
    for (let car of cars) {
        console.log(attribute, car);
    }
}

Output:

Electric Tesla
Electric Porsche
Fast Tesla
Fast Porsche
  • The outer loop iterates through the attributes array. It first picks "Electric", then "Fast".
  • For each iteration of the outer loop, the inner loop runs through all the items in the cars list.

Building a Square Pattern

Let’s say you want to print a square pattern like this:

* * *  
* * *  
* * *  
  • The outer loop controls the number of rows (3 rows).
  • The inner loop controls how many stars are printed per row (3 stars per row).

for (let i = 0; i < 3; i++) {  
    let row = "";
    for (let j = 0; j < 3; j++) {  
        row += "* ";
    }
    console.log(row);
}


Building a Right-Angled Triangle Pattern

Let’s say you want to print a right-angled triangle pattern like this:

*  
* *  
* * *  
* * * *  
* * * * *  
  • The outer loop controls the number of rows (5 rows).
  • The inner loop prints a number of stars equal to the current row number.

let height = 5;
for (let i = 1; i <= height; i++) {  
    let row = "";
    for (let j = 0; j < i; j++) {  
        row += "* ";
    }
    console.log(row);
}


Building an Inverted Right-Angled Triangle Pattern

Now, let’s print an inverted right-angled triangle:

* * * * *  
* * * *  
* * *  
* *  
*  
  • The outer loop starts at 5 and decreases to 1.
  • The inner loop prints stars equal to the current row number.

let height = 5;
for (let i = height; i > 0; i--) {  
    let row = "";
    for (let j = 0; j < i; j++) {  
        row += "* ";
    }
    console.log(row);
}


Building a Hollow Square Pattern

A hollow square has stars only on the borders, with spaces inside:

* * * * *  
*       *  
*       *  
*       *  
* * * * *  
  • The first and last rows contain only stars.
  • The middle rows have stars only at the beginning and end, with spaces inside.

let size = 5;
for (let i = 0; i < size; i++) {  
    let row = "";
    for (let j = 0; j < size; j++) {  
        if (i === 0 || i === size - 1 || j === 0 || j === size - 1) {
            row += "* ";
        } else {
            row += "  ";  
        }
    }
    console.log(row);
}


Takeaways

  • Nested loops are useful for working with multi-dimensional structures.
  • The outer loop controls the number of rows.
  • The inner loop controls how many items are printed per row.
  • By combining loops and conditions, you can create complex patterns like squares, triangles, and grids.