Category: Subjects

K Maps | karnaugh Maps | Solved Examples

Minimization Of Boolean Expressions-

 

There are following two methods of minimizing or reducing the boolean expressions-

 

 

  1. By using laws of Boolean Algebra
  2. By using Karnaugh Maps also called as K Maps

 

In this article, we will discuss about Karnaugh Maps or K Maps.

 

Karnaugh Map-

 

The Karnaugh Map also called as K Map is a graphical representation

that provides a systematic method for simplifying the boolean expressions.

 

For a boolean expression consisting of n-variables, number of cells required in K Map = 2n cells.

 

Two Variable K Map-

 

  • Two variable K Map is drawn for a boolean expression consisting of two variables.
  • The number of cells present in two variable K Map = 22 = 4 cells.
  • So, for a boolean function consisting of two variables, we draw a 2 x 2 K Map.

 

Two variable K Map may be represented as-

 

 

Here, A and B are the two variables of the given boolean function.

 

Three Variable K Map-

 

  • Three variable K Map is drawn for a boolean expression consisting of three variables.
  • The number of cells present in three variable K Map = 23 = 8 cells.
  • So, for a boolean function consisting of three variables, we draw a 2 x 4 K Map.

 

Three variable K Map may be represented as-

 

 

Here, A, B and C are the three variables of the given boolean function.

 

Four Variable K Map-

 

  • Four variable K Map is drawn for a boolean expression consisting of four variables.
  • The number of cells present in four variable K Map = 24 = 16 cells.
  • So, for a boolean function consisting of four variables, we draw a 4 x 4 K Map.

 

Four variable K Map may be represented as-

 

 

Here, A, B, C and D are the four variables of the given boolean function.

 

Karnaugh Map Simplification Rules-

 

To minimize the given boolean function,

  • We draw a K Map according to the number of variables it contains.
  • We fill the K Map with 0’s and 1’s according to its function.
  • Then, we minimize the function in accordance with the following rules.

 

Rule-01:

 

  • We can either group 0’s with 0’s or 1’s with 1’s but we can not group 0’s and 1’s together.
  • X representing don’t care can be grouped with 0’s as well as 1’s.

 

NOTE

There is no need of separately grouping X’s i.e. they can be ignored if all 0’s and 1’s are already grouped.

 

Rule-02:

 

  • Groups may overlap each other.

 

Rule-03:

 

  • We can only create a group whose number of cells can be represented in the power of 2.
  • In other words, a group can only contain 2n i.e. 1, 2, 4, 8, 16 and so on number of cells.

 

Example-

 

 

Rule-04:

 

  • Groups can be only either horizontal or vertical.
  • We can not create groups of diagonal or any other shape.

 

 

Rule-05:

 

  • Each group should be as large as possible.

 

Example-

 

 

Rule-06:

 

  • Opposite grouping and corner grouping are allowed.
  • The example of opposite grouping is shown illustrated in Rule-05.
  • The example of corner grouping is shown below.

 

Example-

 

 

Rule-07:

 

  • There should be as few groups as possible.

 

PROBLEMS BASED ON KARNAUGH MAP-

 

Problem-01:

 

Minimize the following boolean function-

F(A, B, C, D) = Σm(0, 1, 2, 5, 7, 8, 9, 10, 13, 15)

 

Solution-

 

  • Since the given boolean expression has 4 variables, so we draw a 4 x 4 K Map.
  • We fill the cells of K Map in accordance with the given boolean function.
  • Then, we form the groups in accordance with the above rules.

 

Then, we have-

 

 

Now,

F(A, B, C, D)

= (A’B + AB)(C’D + CD) + (A’B’ + A’B + AB + AB’)C’D + (A’B’ + AB’)(C’D’ + CD’)

= BD + C’D + B’D’

 

Thus, minimized boolean expression is-

F(A, B, C, D) = BD + C’D + B’D’

 

Problem-02:

 

Minimize the following boolean function-

F(A, B, C, D) = Σm(0, 1, 3, 5, 7, 8, 9, 11, 13, 15)

 

Solution-

 

  • Since the given boolean expression has 4 variables, so we draw a 4 x 4 K Map.
  • We fill the cells of K Map in accordance with the given boolean function.
  • Then, we form the groups in accordance with the above rules.

 

Then, we have-

 

 

Now,

F(A, B, C, D)

= (A’B’ + A’B + AB + AB’)(C’D + CD) + (A’B’ + AB’)(C’D’ + C’D)

= D + B’C’

 

Thus, minimized boolean expression is-

F(A, B, C, D) = B’C’ + D

 

Problem-03:

 

Minimize the following boolean function-

F(A, B, C, D) = Σm(1, 3, 4, 6, 8, 9, 11, 13, 15) + Σd(0, 2, 14)

 

Solution-

 

  • Since the given boolean expression has 4 variables, so we draw a 4 x 4 K Map.
  • We fill the cells of K Map in accordance with the given boolean function.
  • Then, we form the groups in accordance with the above rules.

 

Then, we have-

 

 

Now,

F(A, B, C, D)

= (AB + AB’)(C’D + CD) + (A’B’ + AB’)(C’D + CD) + (A’B’ + AB’)(C’D’ + C’D) + (A’B’ + A’B)(C’D’ + CD’)

= AD + B’D + B’C’ + A’D’

 

Thus, minimized boolean expression is-

F(A, B, C, D) = AD + B’D + B’C’ + A’D’

 

Problem-04:

 

Minimize the following boolean function-

F(A, B, C) = Σm(0, 1, 6, 7) + Σd(3, 5)

 

Solution-

 

  • Since the given boolean expression has 3 variables, so we draw a 2 x 4 K Map.
  • We fill the cells of K Map in accordance with the given boolean function.
  • Then, we form the groups in accordance with the above rules.

 

Then, we have-

 

 

Now,

F(A, B, C)

= A'(B’C’ + B’C) + A(BC + BC’)

= A’B’ + AB

 

Thus, minimized boolean expression is-

F(A, B, C) = AB + A’B’

 

NOTE-

 

  • It may be noted that there is no need of considering the quad group.
  • This is because even if we consider that group, we will have to consider the other two duets.
  • So, there is no use of considering that quad group.

 

Problem-05:

 

Minimize the following boolean function-

F(A, B, C) = Σm(1, 2, 5, 7) + Σd(0, 4, 6)

 

Solution-

 

  • Since the given boolean expression has 3 variables, so we draw a 2 x 4 K Map.
  • We fill the cells of K Map in accordance with the given boolean function.
  • Then, we form the groups in accordance with the above rules.

 

Then, we have-

 

 

Now,

F(A, B, C)

= (A + A’)(B’C’ + B’C) + A(B’C’ + B’C + BC + BC’) + (A + A’)(B’C’ + BC’)

= B’ + A + C’

 

Thus, minimized boolean expression is-

F(A, B, C) = A + B’ + C’

 

Problem-06:

 

Minimize the following boolean function-

F(A, B, C) = Σm(0, 1, 6, 7) + Σd(3, 4, 5)

 

Solution-

 

  • Since the given boolean expression has 3 variables, so we draw a 2 x 4 K Map.
  • We fill the cells of K Map in accordance with the given boolean function.
  • Then, we form the groups in accordance with the above rules.

 

Then, we have-

 

 

Now,

F(A, B, C)

= (A + A’)(B’C’ + B’C) + A(B’C’ + B’C + BC + BC’)

= B’ + A

 

Thus, minimized boolean expression is-

F(A, B, C) = A + B’

 

Problem-07:

 

Minimize the following boolean function-

F(A, B, C, D) = Σm(0, 2, 8, 10, 14) + Σd(5, 15)

 

Solution-

 

  • Since the given boolean expression has 4 variables, so we draw a 4 x 4 K Map.
  • We fill the cells of K Map in accordance with the given boolean function.
  • Then, we form the groups in accordance with the above rules.

 

Then, we have-

 

 

Now,

F(A, B, C, D)

= (AB + AB’)CD’ + (A’B’ + AB’)(C’D’ + CD’)

= ACD’ + B’D’

 

Thus, minimized boolean expression is-

F(A, B, C, D) = ACD’ + B’D’

 

Problem-08:

 

Minimize the following boolean function-

F(A, B, C, D) = Σm(3, 4, 5, 7, 9, 13, 14, 15)

 

Solution-

 

  • Since the given boolean expression has 4 variables, so we draw a 4 x 4 K Map.
  • We fill the cells of K Map in accordance with the given boolean function.
  • Then, we form the groups in accordance with the above rules.

 

Then, we have-

 

 

Now,

F(A, B, C, D)

= A’B(C’D’ + C’D) + (A’B’ + A’B)(CD) + (AB + AB’)(C’D) + AB(CD + CD’)

= A’BC’ + A’CD + AC’D + ABC

 

Thus, minimized boolean expression is-

F(A, B, C, D) = A’BC’ + A’CD + AC’D + ABC

 

It is important to note that we are not considering the quad group because we have to consider the duets anyhow.

 

Problem-09:

 

Consider the following boolean function-

F(W, X, Y, Z) = Σm(1, 3, 4, 6, 9, 11, 12, 14)

 

This function is independent ________ number of variables. Fill in the blank.

 

Solution-

 

  • Since the given boolean expression has 4 variables, so we draw a 4 x 4 K Map.
  • We fill the cells of K Map in accordance with the given boolean function.
  • Then, we form the groups in accordance with the above rules.

 

Then, we have-

 

 

Now,

F(W, X, Y, Z)

= (W’X + WX)(Y’Z’ + YZ’) + (W’X’ + WX’)(Y’Z + YZ)

= XZ’ + X’Z

= X ⊕ Z

 

Thus, minimized boolean expression is-

F(W, X, Y, Z) = X ⊕ Z

 

Clearly, the given boolean function depends on only two variables X and Z.

Hence, it is independent of other two variables W and Y.

 

Next Article- Neutral Functions

 

Get more notes and other study material of Digital Design.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Candidate Key | Finding Candidate Keys | Examples

Keys in DBMS-

 

Before you go through this article, make sure that you have gone through the previous article on Keys in DBMS.

 

We have discussed-

  • A key is a set of attributes that can identify each tuple uniquely in the given relation.
  • There are various types of keys in DBMS-

 

 

In this article, we will discuss how to find candidate keys of a given relation.

 

Candidate Key-

 

A candidate key may be defined as-

 

A set of minimal attribute(s) that can identify each tuple uniquely in the given relation is called as a candidate key.

OR

A minimal super key is called as a candidate key.

 

For any given relation,

  • It is possible to have multiple candidate keys.
  • There exists no general formula to find the total number of candidate keys of a given relation.

 

Example-

 

Consider the following Student schema-

Student ( roll , name , sex , age , address , class , section )

 

Given below are the examples of candidate keys-

  • ( class , section , roll )
  • ( name , address )

 

These are candidate keys because each set consists of minimal attributes required to identify each student uniquely in the Student table.

 

Finding Candidate Keys-

 

We can determine the candidate keys of a given relation using the following steps-

 

Step-01:

 

  • Determine all essential attributes of the given relation.
  • Essential attributes are those attributes which are not present on RHS of any functional dependency.
  • Essential attributes are always a part of every candidate key.
  • This is because they can not be determined by other attributes.

 

Example

 

Let R(A, B, C, D, E, F) be a relation scheme with the following functional dependencies-

A → B

C → D

D → E

 

Here, the attributes which are not present on RHS of any functional dependency are A, C and F.

So, essential attributes are- A, C and F.

 

Step-02:

 

  • The remaining attributes of the relation are non-essential attributes.
  • This is because they can be determined by using essential attributes.

 

Now, following two cases are possible-

 

Case-01:

 

If all essential attributes together can determine all remaining non-essential attributes, then-

  • The combination of essential attributes is the candidate key.
  • It is the only possible candidate key.

 

Case-02:

 

If all essential attributes together can not determine all remaining non-essential attributes, then-

  • The set of essential attributes and some non-essential attributes will be the candidate key(s).
  • In this case, multiple candidate keys are possible.
  • To find the candidate keys, we check different combinations of essential and non-essential attributes.

 

We will further understand how to find candidate keys with the help of following problems.

The following practice problems are based on Case-01.

 

PRACTICE PROBLEMS BASED ON FINDING CANDIDATE KEYS-

 

Problem-01:

 

Let R = (A, B, C, D, E, F) be a relation scheme with the following dependencies-

C → F

E → A

EC → D

A → B

Which of the following is a key for R?

  1. CD
  2. EC
  3. AE
  4. AC

 

Also, determine the total number of candidate keys and super keys.

 

Solution-

 

We will find candidate keys of the given relation in the following steps-

 

Step-01:

 

  • Determine all essential attributes of the given relation.
  • Essential attributes of the relation are- C and E.
  • So, attributes C and E will definitely be a part of every candidate key.

 

Step-02:

 

Now,

  • We will check if the essential attributes together can determine all remaining non-essential attributes.
  • To check, we find the closure of CE.

 

So, we have-

{ CE }+

= { C , E }

= { C , E , F }                       ( Using C → F )

= { A , C , E , F }                  ( Using E → A )

= { A , C , D , E , F }            ( Using EC → D )

= { A , B , C , D , E , F }       ( Using A → B )

 

We conclude that CE can determine all the attributes of the given relation.

So, CE is the only possible candidate key of the relation.

 

Thus, Option (B) is correct.

 

Total Number of Candidate Keys-

 

Only one candidate key CE is possible.

 

Total Number of Super Keys-

 

There are total 6 attributes in the given relation of which-

  • There are 2 essential attributes- C and E.
  • Remaining 4 attributes are non-essential attributes.
  • Essential attributes will be definitely present in every key.
  • Non-essential attributes may or may not be taken in every super key.

 

 

So, number of super keys possible = 2 x 2 x 2 x 2 = 16.

Thus, total number of super keys possible = 16.

 

Problem-02:

 

Let R = (A, B, C, D, E) be a relation scheme with the following dependencies-

AB → C

C → D

B → E

Determine the total number of candidate keys and super keys.

 

Solution-

 

We will find candidate keys of the given relation in the following steps-

 

Step-01:

 

  • Determine all essential attributes of the given relation.
  • Essential attributes of the relation are- A and B.
  • So, attributes A and B will definitely be a part of every candidate key.

 

Step-02:

 

Now,

  • We will check if the essential attributes together can determine all remaining non-essential attributes.
  • To check, we find the closure of AB.

 

So, we have-

{ AB }+

= { A , B }

= { A , B , C }                     ( Using AB → C )

= { A , B , C , D }               ( Using C → D )

= { A , B , C , D , E }          ( Using B → E )

 

We conclude that AB can determine all the attributes of the given relation.

Thus, AB is the only possible candidate key of the relation.

 

Total Number of Candidate Keys-

 

Only one candidate key AB is possible.

 

Total Number of Super Keys-

 

There are total 5 attributes in the given relation of which-

  • There are 2 essential attributes- A and B.
  • Remaining 3 attributes are non-essential attributes.
  • Essential attributes will be definitely present in every key.
  • Non-essential attributes may or may not be taken in every super key.

 

 

So, number of super keys possible = 2 x 2 x 2 = 8.

Thus, total number of super keys possible = 8.

 

Problem-03:

 

Consider the relation scheme R(E, F, G, H, I, J, K, L, M, N) and the set of functional dependencies-

{ E, F } → { G }

{ F } → { I , J }

{ E, H } → { K, L }

{ K } → { M }

{ L } → { N }

 

What is the key for R?

  1. { E, F }
  2. { E, F, H }
  3. { E, F, H, K, L }
  4. { E }

 

Also, determine the total number of candidate keys and super keys.

 

Solution-

 

We will find candidate keys of the given relation in the following steps-

 

Step-01:

 

  • Determine all essential attributes of the given relation.
  • Essential attributes of the relation are- E, F and H.
  • So, attributes E, F and H will definitely be a part of every candidate key.

 

Step-02:

 

Now,

  • We will check if the essential attributes together can determine all remaining non-essential attributes.
  • To check, we find the closure of EFH.

 

So, we have-

{ EFH }+

= { E , F , H }

= { E , F , G , H }                                ( Using EF → G )

= { E , F , G , H , I , J }                       ( Using F → IJ )

= { E , F , G , H , I , J , K , L }             ( Using EH → KL )

= { E , F , G , H , I , J , K , L , M }       ( Using K → M )

= { E , F , G , H , I , J , K , L , M , N } ( Using L → N )

 

We conclude that EFH can determine all the attributes of the given relation.

So, EFH is the only possible candidate key of the relation.

 

Thus, Option (B) is correct.

 

Total Number of Candidate Keys-

 

Only one candidate key EFH is possible.

 

Total Number of Super Keys-

 

There are total 10 attributes in the given relation of which-

  • There are 3 essential attributes- E, F and H.
  • Remaining 7 attributes are non-essential attributes.
  • Essential attributes will be definitely present in every key.
  • Non-essential attributes may or may not be taken in every super key.

 

 

So, number of super keys possible = 2 x 2 x 2 x 2 x 2 x 2 x 2 = 128.

Thus, total number of super keys possible = 128.

 

Problem-04:

 

Consider the relation scheme R(A, B, C, D, E, H) and the set of functional dependencies-

A → B

BC → D

E → C

D → A

 

What are the candidate keys of R?

  1. AE, BE
  2. AE, BE, DE
  3. AEH, BEH, BCH
  4. AEH, BEH, DEH

 

Solution-

 

We will find candidate keys of the given relation in the following steps-

 

Step-01:

 

  • Determine all essential attributes of the given relation.
  • Essential attributes of the relation are- E and H.
  • So, attributes E and H will definitely be a part of every candidate key.

 

The only possible option is (D).

Thus, Option (D) is correct.

 

Next Article- Functional Dependency in DBMS

 

Get more notes and other study material of Database Management System (DBMS).

Watch video lectures by visiting our YouTube channel LearnVidFun.

Cause Effect Graph Technique | Examples

Cause Effect Graph-

 

  • Cause Effect Graph is a popular black box testing technique.
  • It illustrates the relationship between a given outcome and all the factors that influence the outcome graphically.

 

 

Here,

  • A “Cause” stands for a distinct input condition that fetches about an internal change in the system.
  • An “Effect” represents an output condition, a system state that results from a combination of causes.

 

Applications-

 

  • For analyzing the existing problem so that corrective actions can be taken at the earliest.
  • For relating the interactions of the system with the factors affecting a particular process.
  • For identifying the possible root causes, reasons for a particular effect, problem or outcome.

 

Advantages-

 

  • It helps to determine the root causes of a problem or quality.
  • It indicates possible causes of variation in a process.
  • It identifies those areas where data should be collected for further study.
  • It utilizes the team knowledge of the process by encouraging team participation.

 

Steps For Drawing Cause Effect Diagram-

 

The following steps are followed-

  • Identify and describe the input conditions (causes) and actions (effect).
  • Build up a cause-effect graph.
  • Convert cause-effect graph into a decision table.
  • Convert decision table rules to test cases where each column of the decision table represents a test case.

 

PRACTICE PROBLEMS BASED ON CAUSE-EFFECT GRAPH TECHNIQUE-

 

Problem-01:

 

Design test cases for the following problem-

If the character of the first column is ‘A’ or ‘B’ and the second column is a number, then the file is considered updated. If the first character is erroneous, then message x should be printed. If the second column is not a number, then message y should be printed.

 

Solution-

 

Step-01:

 

Identify and describe the input conditions (causes) and actions (effect).

 

The causes represented by letter “C” are as follows-

  • C1 : The character in column 1 is ‘A’
  • C2 : The character in column 1 is ‘B’
  • C3 : The character in column 2 is a number

 

The effects represented by letter “e” are as follows-

  • e1 : File update is made
  • e2 : Message x is printed
  • e3 : Message y is printed

 

Step-02:

 

Build up a cause-effect graph-

 

 

Step-03:

 

Convert cause-effect graph into a decision table-

 

Test data Causes Effect
A1 A2 A3 M1 M2 M3
1 0 0 0 0 1 1
2 0 0 1 0 1 0
3 0 1 0 0 0 1
4 0 1 1 1 0 0
5 1 0 0 0 0 1
6 1 0 1 1 0 0

 

Problem-02:

 

Why Cause Effect Graphing Technique is Better Than Any Other Black Box Testing Technique?

 

Solution-

 

  • Boundary value analysis and equivalence partitioning do not explore combinations of input circumstances.
  • These only consider the single input conditions.
  • However, combinations of inputs may result in interesting situations.
  • These situations should be tested.
  • By considering all the valid combinations of equivalence classes, there will be large number of test cases.
  • Many of these test cases will not be useful for revealing any new errors.

 

On the other hand,

  • Cause Effect Graph is a technique that helps in selecting a high-yield set of test cases in a systematic way.
  • It has a beneficial effect in pointing out incompleteness and ambiguities in the specifications.

 

To gain better understanding about Cause Effect Graph Technique,

Watch this Video Lecture

 

Get more notes and other study material of Software Engineering.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Cyclomatic Complexity | Calculation | Examples

Cyclomatic Complexity-

 

Cyclomatic Complexity may be defined as-

  • It is a software metric that measures the logical complexity of the program code.
  • It counts the number of decisions in the given program code.
  • It measures the number of linearly independent paths through the program code.

 

Cyclomatic complexity indicates several information about the program code-

 

Cyclomatic Complexity Meaning
1 – 10
  • Structured and Well Written Code
  • High Testability
  • Less Cost and Effort
10 – 20
  • Complex Code
  • Medium Testability
  • Medium Cost and Effort
20 – 40
  • Very Complex Code
  • Low Testability
  • High Cost and Effort
> 40
  • Highly Complex Code
  • Not at all Testable
  • Very High Cost and Effort

 

Importance of Cyclomatic Complexity-

 

  • It helps in determining the software quality.
  • It is an important indicator of program code’s readability, maintainability and portability.
  • It helps the developers and testers to determine independent path executions.
  • It helps to focus more on the uncovered paths.
  • It evaluates the risk associated with the application or program.
  • It provides assurance to the developers that all the paths have been tested at least once.

 

Properties of Cyclomatic Complexity-

 

  • It is the maximum number of independent paths through the program code.
  • It depends only on the number of decisions in the program code.
  • Insertion or deletion of functional statements from the code does not affect its cyclomatic complexity.
  • It is always greater than or equal to 1.

 

Calculating Cyclomatic Complexity-

 

Cyclomatic complexity is calculated using the control flow representation of the program code.

 

In control flow representation of the program code,

  • Nodes represent parts of the code having no branches.
  • Edges represent possible control flow transfers during program execution

 

There are 3 commonly used methods for calculating the cyclomatic complexity-

 

Method-01:

 

Cyclomatic Complexity = Total number of closed regions in the control flow graph + 1

 

Method-02:

 

Cyclomatic Complexity = E – N + 2

 

Here-

  • E = Total number of edges in the control flow graph
  • N = Total number of nodes in the control flow graph

 

Method-03:

 

Cyclomatic Complexity = P + 1

 

Here,

P = Total number of predicate nodes contained in the control flow graph

 

Note-

 

  • Predicate nodes are the conditional nodes.
  • They give rise to two branches in the control flow graph.

 

PRACTICE PROBLEMS BASED ON CYCLOMATIC COMPLEXITY-

 

Problem-01:

 

Calculate cyclomatic complexity for the given code-

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
IF A = 354
THEN IF B > C
THEN A = B
ELSE A = C
END IF
END IF
PRINT A
IF A = 354 THEN IF B > C THEN A = B ELSE A = C END IF END IF PRINT A
IF A = 354
   THEN IF B > C
        THEN A = B
        ELSE A = C
   END IF
END IF
PRINT A

 

Solution-

 

We draw the following control flow graph for the given code-

 

 

Using the above control flow graph, the cyclomatic complexity may be calculated as-

 

Method-01:

 

Cyclomatic Complexity

= Total number of closed regions in the control flow graph + 1

= 2 + 1

= 3

 

Method-02:

 

Cyclomatic Complexity

= E – N + 2

= 8 – 7 + 2

= 3

 

Method-03:

 

Cyclomatic Complexity

= P + 1

= 2 + 1

= 3

 

Problem-02:

 

Calculate cyclomatic complexity for the given code-

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{ int i, j, k;
for (i=0 ; i<=N ; i++)
p[i] = 1;
for (i=2 ; i<=N ; i++)
{
k = p[i]; j=1;
while (a[p[j-1]] > a[k] {
p[j] = p[j-1];
j--;
}
p[j]=k;
}
{ int i, j, k; for (i=0 ; i<=N ; i++) p[i] = 1; for (i=2 ; i<=N ; i++) { k = p[i]; j=1; while (a[p[j-1]] > a[k] { p[j] = p[j-1]; j--; } p[j]=k; }
{ int i, j, k;
  for (i=0 ; i<=N ; i++)
  p[i] = 1;
  for (i=2 ; i<=N ; i++)
  {
     k = p[i]; j=1;
     while (a[p[j-1]] > a[k] {
         p[j] = p[j-1];
         j--;
  }
     p[j]=k;
}

 

Solution-

 

We draw the following control flow graph for the given code-

 

 

Using the above control flow graph, the cyclomatic complexity may be calculated as-

 

Method-01:

 

Cyclomatic Complexity

= Total number of closed regions in the control flow graph + 1

= 3 + 1

= 4

 

Method-02:

 

Cyclomatic Complexity

= E – N + 2

= 16 – 14 + 2

= 4

 

Method-03:

 

Cyclomatic Complexity

= P + 1

= 3 + 1

= 4

 

Problem-03:

 

Calculate cyclomatic complexity for the given code-

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
begin int x, y, power;
float z;
input(x, y);
if(y<0)
power = -y;
else power = y;
z=1;
while(power!=0)
{ z=z*x;
power=power-1;
} if(y<0)
z=1/z;
output(z);
end
begin int x, y, power; float z; input(x, y); if(y<0) power = -y; else power = y; z=1; while(power!=0) { z=z*x; power=power-1; } if(y<0) z=1/z; output(z); end
begin int x, y, power;
      float z;
      input(x, y);
      if(y<0)
      power = -y;
      else power = y;
      z=1;
      while(power!=0)
      {    z=z*x;
           power=power-1;
      } if(y<0)
      z=1/z;
      output(z);
      end

 

Solution-

 

We draw the following control flow graph for the given code-

 

 

Using the above control flow graph, the cyclomatic complexity may be calculated as-

 

Method-01:

 

Cyclomatic Complexity

= Total number of closed regions in the control flow graph + 1

= 3 + 1

= 4

 

Method-02:

 

Cyclomatic Complexity

= E – N + 2

= 16 – 14 + 2

= 4

 

Method-03:

 

Cyclomatic Complexity

= P + 1

= 3 + 1

= 4

 

To gain better understanding about Cyclomatic Complexity,

Watch this Video Lecture

 

Next Article- Cause Effect Graph Technique

 

Get more notes and other study material of Software Engineering.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Linear Regression Machine Learning | Examples

Linear Regression-

 

In Machine Learning,

  • Linear Regression is a supervised machine learning algorithm.
  • It tries to find out the best linear relationship that describes the data you have.
  • It assumes that there exists a linear relationship between a dependent variable and independent variable(s).
  • The value of the dependent variable of a linear regression model is a continuous value i.e. real numbers.

 

Also Read- Machine Learning Algorithms

 

Representing Linear Regression Model-

 

Linear regression model represents the linear relationship between a dependent variable and independent variable(s) via a sloped straight line.

 

 

The sloped straight line representing the linear relationship that fits the given data best is called as a regression line.

It is also called as best fit line.

 

Types of Linear Regression-

 

Based on the number of independent variables, there are two types of linear regression-

 

 

  1. Simple Linear Regression
  2. Multiple Linear Regression

 

1. Simple Linear Regression-

 

In simple linear regression, the dependent variable depends only on a single independent variable.

 

For simple linear regression, the form of the model is-

Y = β0 + β1X

 

Here,

  • Y is a dependent variable.
  • X is an independent variable.
  • β0 and β1 are the regression coefficients.
  • β0 is the intercept or the bias that fixes the offset to a line.
  • β1 is the slope or weight that specifies the factor by which X has an impact on Y.

 

There are following 3 cases possible-

 

Case-01: β1 < 0

 

  • It indicates that variable X has negative impact on Y.
  • If X increases, Y will decrease and vice-versa.

 

 

Case-02: β1 = 0

 

  • It indicates that variable X has no impact on Y.
  • If X changes, there will be no change in Y.

 

 

Case-03: β1 > 0

 

  • It indicates that variable X has positive impact on Y.
  • If X increases, Y will increase and vice-versa.

 

 

2. Multiple Linear Regression-

 

In multiple linear regression, the dependent variable depends on more than one independent variables.

 

For multiple linear regression, the form of the model is-

Y = β0 + β1X1 + β2X2 + β3X3 + …… + βnXn

 

Here,

  • Y is a dependent variable.
  • X1, X2, …., Xn are independent variables.
  • β0, β1,…, βn are the regression coefficients.
  • βj (1<=j<=n) is the slope or weight that specifies the factor by which Xj has an impact on Y.

 

To gain better understanding about Linear Regression,

Watch this Video Lecture

 

Get more notes and other study material of Machine Learning.

Watch video lectures by visiting our YouTube channel LearnVidFun.