Friday, June 28, 2024

Krushkal algorithm

 //Program of krushkal algorithm


#include <stdio.h>

#include <stdlib.h>


#define V 5 // Number of vertices

#define E 6 // Number of edges


// Structure to represent a graph edge

typedef struct Edge {

  int u, v, weight;

} Edge;


// Structure to represent a disjoint set

typedef struct DisjointSet {

  int parent[V];

} DisjointSet;


// Function to create a disjoint set

DisjointSet* createDisjointSet() {

  DisjointSet* ds = (DisjointSet*) malloc(sizeof(DisjointSet));

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

    ds->parent[i] = i;

  }

  return ds;

}


// Function to find the parent of a vertex

int find(DisjointSet* ds, int i) {

  if (ds->parent[i] == i) return i;

  return find(ds, ds->parent[i]);

}


// Function to merge two sets

void union1(DisjointSet* ds, int x, int y) {

  int xroot = find(ds, x);

  int yroot = find(ds, y);

  ds->parent[xroot] = yroot;

}


// Function to implement Kruskal's algorithm

void kruskal(Edge* edges, int numEdges) {

  DisjointSet* ds = createDisjointSet();

  Edge* result = (Edge*) malloc(sizeof(Edge) * (V-1));

  int i, j;

  for (i = 0; i < V-1; i++) {

    int min = -1;

    for (j = 0; j < numEdges; j++) {

      if (find(ds, edges[j].u) != find(ds, edges[j].v)) {

        if (min == -1 || edges[j].weight < edges[min].weight) {

          min = j;

        }

      }

    }

    result[i] = edges[min];

    union1(ds, edges[min].u, edges[min].v);

  }

  printf("Minimum Spanning Tree:\n");

  for (i = 0; i < V-1; i++) {

    printf("%d - %d = %d\n", result[i].u, result[i].v, result[i].weight);

  }

}


int main() {

  Edge edges[] = {

    {0, 1, 2},

    {0, 2, 4},

    {1, 2, 1},

    {1, 3, 3},

    {2, 3, 5},

    {3, 4, 2}

  };

  int numEdges = sizeof(edges) / sizeof(edges[0]);

  kruskal(edges, numEdges);

  return 0;

}


Tuesday, June 4, 2024

HTML program for countdown timer

 <!DOCTYPE HTML>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<style>

p {

  text-align: center;

  font-size: 60px;

  margin-top: 0px;

}

</style>

</head>

<body>


<p id="demo"></p>


<script>

// Set the date we're counting down to

var countDownDate = new Date("Jan 5, 2030 15:37:25").getTime();


// Update the count down every 1 second

var x = setInterval(function() {


  // Get today's date and time

  var now = new Date().getTime();

    

  // Find the distance between now and the count down date

  var distance = countDownDate - now;

    

  // Time calculations for days, hours, minutes and seconds

  var days = Math.floor(distance / (1000 * 60 * 60 * 24));

  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));

  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));

  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    

  // Output the result in an element with id="demo"

  document.getElementById("demo").innerHTML = days + "d " + hours + "h "

  + minutes + "m " + seconds + "s ";

    

  // If the count down is over, write some text 

  if (distance < 0) {

    clearInterval(x);

    document.getElementById("demo").innerHTML = "EXPIRED"

;

  }

}, 1000);

</script>


</body>

</html>

  Blog chatgpt Blog