"Algorithm: C language Implementation" reading notes

Source: Internet
Author: User

Prepare to read the book carefully from today. Although the slag is stupid, but the nest lazy ah ....

Start reading the first chapter today. Hope to keep going.

Chapter I. INTRODUCTION

By discussing several algorithms of connectivity problem, the importance of the algorithm is derived.

A fast search algorithm for 1.1 connectivity problems

The feeling is that each point is dyed, each color represents a pile, interconnected with each other. Each time you enter two points, change the color of the two points to the same so that they are connected to each other.

Time complexity: O (MN), M is the number of input instructions, n is the number of points

1.1 Fast Lookup algorithm for connectivity problems # include <stdio.h> #define N-id[n];                          Represents the color of each point int main () {    //freopen ("In.txt", "R", stdin);    int i, t, p, Q;     for (i = 0; i < N; ++i)        id[i] = i;                  Start every two points are not connected, so each point a color while    (scanf ("%d%d", &p, &q) = = 2) {        if (id[p]! = Id[q]) {            for (t = id[p], i = 0; i < N; ++i)                if (id[i] = = t)     //Put all and p a color Dianran into Q's color                    id[i] = id[q];        }        for (i = 0; i < N; ++i)            printf ("%d", Id[i]);        printf ("\ n");    }    return 0;}

Fast merging solution of 1.2 connectivity problems

It is two points in the same place two points on the same tree, so that two points of the same root for their connectivity. Each time a root of two points is found, if it is not the same, one is attached to the other root.

Time complexity: O (MN), M is the number of input instructions, n is the number of points. When M>n, the number of executions is MN/2

1.2 Fast merging algorithm for connectivity problems # include <stdio.h> #define N int main () {    //freopen ("In.txt", "R", stdin);    int I, J, p, Q;    int id[n];                      Represents the parent node for each point for    (i = 0; i < N; ++i)        id[i] = i;                  Start every two points are not connected, so the parent node of each point is itself while    (scanf ("%d%d", &p, &q) = = 2) {for        (i = p; I! = Id[i]; i = id[i])            /*n othing*/;           When the parent node of the node is equal to the node, it proves that the node is a root for        (j = q; J! = Id[j]; j = id[j])            /*nothing*/;        if (i! = j)                 //At this time I is the root of P, J is the root of Q            id[i] = j;        for (i = 0; i < N; ++i)            printf ("%d", Id[i]);        printf ("\ n");    }    return 0;}

1.3 Weighted fast Merging algorithm

Record the number of nodes in each tree, and connect the root of the node to more nodes.

"Algorithm: C language Implementation" reading notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.