What Is Breadth-First Search?

Why Should I Care?

A lot of algorithms are implemented for you as part of your chosen language. That means that they are interesting to learn about, but you'll rarely write them yourself.

Graph traversal algorithms are different. We use graphs all the time, from linking related products in an e-commerce application to mapping relationships between people in a social network.

SKP’s Algorithms and Data Structures #9: Java Problem: Monkeys in the Garden

[Question/Problem Statement is the Property of Techgig] 

Monkeys in the Garden [www.techgig.com]

In a garden, trees are arranged in a circular fashion with an equal distance between two adjacent trees. The height of trees may vary. Two monkeys live in that garden and they were very close to each other. One day they quarreled due to some misunderstanding. None of them were ready to leave the garden. But each one of them wants that if the other wants to meet him, it should take maximum possible time to reach him, given that they both live in the same garden.

SKP’s Algorithms and Data Structures #8: Java Problem: Simple Inheritance (OOPs)

[Question/Problem Statement is the Property of Techgig]
 
Java Inheritance / Simple OOPs [www.techgig.com]
Create Two Classes:

BaseClass
The Rectangle class should have two data fields-width and height of int types. The class should have display() method, to print the width and height of the rectangle separated by space.

DerivedClass
The RectangleArea class is Derived from Rectangle class, i.e., it is the Sub-Class of Rectangle class. The class should have read_input() method, to Read the Values of width and height of the Rectangle. The RectangleArea class should also Overload the display() Method to Print the Area (width*height) of the Rectangle.

Input Format
The First and Only Line of Input contains two space-separated Integers denoting the width and height of the Rectangle.

Constraints
1 <= width,height <= 10^3

Output Format
The Output Should Consist of Exactly Two Lines.
In the First Line, Print the Width and Height of the Rectangle Separated by Space.
In the Second Line, Print the Area of the Rectangle.


[Explanation of the Solution]
This is the Simplest of all OOPs Questions! Demonstration of Inheritance and Overriding (Very Loosely, Liskov Substitution of SOLID).


[Source Code, Sumith Puri (c) 2021 — Free to Use and Distribute]
Java
 




x
67


1
 /*    
2
  * Techgig Core Java Basics Problem - Get Simple OOPs Right!  
3
  * Author: Sumith Puri [I Bleed Java!]; GitHub: @sumithpuri;  
4
  */   
5
     
6
  import java.io.*;   
7
  import java.util.*;   
8
   
9
   
10
  class Rectangle {  
11
   
12
    private int width;  
13
    private int height;  
14
   
15
    public void display() {  
16
   
17
      System.out.println(width + " " + height);  
18
    }  
19
   
20
    public int getWidth() {  
21
   
22
      return width;  
23
    }  
24
   
25
    public void setWidth(int width) {  
26
   
27
      this.width=width;  
28
    }  
29
   
30
    public int getHeight() {  
31
   
32
      return height;  
33
    }  
34
   
35
    public void setHeight(int height) {  
36
   
37
      this.height=height;  
38
    }  
39
  }  
40
   
41
  class RectangleArea extends Rectangle {  
42
   
43
    public void read_input() {  
44
   
45
     Scanner scanner = new Scanner (System.in);   
46
       
47
     setWidth(scanner.nextInt());   
48
     setHeight(scanner.nextInt());  
49
    }  
50
   
51
    public void display() {  
52
   
53
      super.display();  
54
      System.out.println(getWidth()*getHeight());  
55
    }  
56
  }  
57
     
58
  public class CandidateCode {   
59
     
60
   public static void main(String args[] ) throws Exception {   
61
     
62
     RectangleArea rectangleArea = new RectangleArea();  
63
     rectangleArea.read_input();  
64
   
65
     rectangleArea.display();  
66
   }   
67
 } 



SKP’s Algorithms and Data Structures #7: Functional Programming and Java Lambdas

[Question/Problem Statement is the Property of Techgig]

Java Advanced — Lambda Expressions [www.techgig.com] 
Write the Following Methods that Return a Lambda Expression Performing a Specified Action: Perform Operation isOdd(): The Lambda Expression must return if a Number is Odd or  If it is Even. Perform Operation isPrime(): The lambda expression must return if a number is prime or if it is composite. PerformOperation isPalindrome(): The Lambda Expression must return if a number is a Palindrome or if it is not.

Input Format
Input is as Show in the Format Below
Input
3
1 3
2 7
3 7777

Constraints
NA

Output Format
Output is as Show in the Format Below
Output
ODD
PRIME
PALINDROME


[Explanation of the Solution]
This is a Good Question to Refresh Java 8 Lambdas. In my Solution, I Implemented the Functional Interfaces within my main() Method and assigned it to Local Reference Variables.


SKP’s Algorithms and Data Structures #6: Java Problem: Active Traders

[Question/Problem Statement is the Property of HackerRank]

Algorithms/Data Structures — [Problem Solving] 
An Institutional Broker wants to Review their Book of Customers to see which are Most Active. Given a List of Trades By "Customer Name, Determine which Customers Account for At Least 5% of the Total Number of Trades. Order the List Alphabetically Ascending By Name."


Example
n = 23
"customers = {"Bigcorp", "Bigcorp", "Acme", "Bigcorp", "Zork", "Zork", "Abe", "Bigcorp", "Acme", "Bigcorp", "Bigcorp", "Zork", "Bigcorp", "Zork", "Zork", "Bigcorp", "Acme", "Bigcorp", "Acme", "Bigcorp", "Acme", "Littlecorp", "Nadircorp"}."

"Bigcorp had 10 Trades out of 23, which is 43.48% of the Total Trades."
"Both Acme and Zork had 5 trades, which is 21.74% of the Total Trades."
"The Littlecorp, Nadircorp, and Abe had 1 Trade Each, which is 4.35%..."

"So the Answer is ["Acme","Bigcorp","Zork"] (In Alphabetical Order) Because only These Three Companies Placed at least 5% of the Trades.


Function Description

Complete the Function mostActive in the Editor Below.

mostActive
has the following parameter:
String customers[n]: An Array Customer Names
(Actual Question Says String Array, But Signature is List of Strings)

SKP’s Algorithms and Data Structures #5: Java Problem: Changes in Usernames

[Question/Problem Statement is the Adapted from HackerRank]

Algorithms/Data Structures — [Problem Solving] 
There is a Specific Need for Changes in a List of Usernames. In a given List of Usernames — For Each Username — If the Username can be Modified and Moved Ahead in a Dictionary. The Allowed Modification is that Alphabets can change Positions in the Given Username.

Example
usernames[] = {"Aba", "Cat"}
 
"Aba" can be Changed to only "Baa" — Hence, It can Never Find a Place Ahead in the Dictionary. Hence, Output will be "NO". "Cat" can be Changed to "Act", "Atc", "Tca", "Tac", "Cta" and Definitely "Act" will Find a Place Before "Cat" in the Dictionary. Hence, Output will be "YES".

[Function Description]
Complete the function possibleChanges in the Editor Below.
 
possibleChanges has the Following Parameters:
String usernames[n]: An Array of User Names
 
Returns String[n]: An Array with "YES" or "NO" Based on Feasibility
(Actual Question Says String Array, But Signature is List of Strings)


Constraints
• [No Special Constraints Exist, But Cannot Recall Exactly]


Input Format 
"The First Line Contains an Integer, n, the Number of Elements in Usernames.",
"Each Line of the n Subsequent Lines (where 0 < i < n) contains a String usernames[i]."        

[Sample Case 0 — Sample Input For Custom Testing]         
5
Aba
Cat
Boby
Buba
Bapg
Sungi
Lapg
Acba
       
Sample Output (Each Should Be on a Separate Line) 
NO YES NO YES YES YES YES YES
   

 
[Explanation of the Solution]
This is again a Good Question from Hacker Rank to Test Your Logic / Problem Solving Abilities. The Core Point to Handle is that For Each Combination of 2 Alphabets that Exists in the Username String > We Need to Check if the Latter Occurring Character (ASCII) is Less than the Former Occurring Character (ASCII). For Example in the String "Bapg" — For a Selection of "Ba" from "Bapg" — We have "a" Occurring Before "B" in the English Alphabet. We can Have Two Loops (One Nested) to Decide for a Combination of Each Two Alphabets. The Time Complexity of this Solution is O(n^2).
 


[Source Code, Sumith Puri (c) 2021 — Free to Use and Distribute]

What Do We Mean by Machine Learning?

The goal of artificial intelligence is to create a machine that can mimic a human mind and to do that, of course, it needs learning capabilities. However, it is more than just about learning. It’s about reasoning, knowledge representation, and even things like abstract thinking. 

Machine learning, on the other hand, is solely focused on writing software that can learn from past experiences. One thing you might find astounding is that machine learning is more closely related to data mining and statistics than it is to AI. Why is that? First, we need to know what we mean by machine learning.

Book Review: Foundations of Deep Reinforcement Learning, by Laura Graesser and Wah Loon Keng

Deep Reinforcement Learning is a somewhat new field within Machine Learning or Artificial Intelligence (you may pick your favorite term between these two, even if they’re not strictly the same), which combines Deep Learning and Reinforcement Learning and is based on the general idea that an agent can learn by observing its actions and their consequences. No, it is not a return to John B. Watson and B. F. Skinner’s behavioral psychology. We are talking, instead, about a set of pretty advanced machine learning algorithms that, when properly mastered, allow computers to achieve remarkable results in some complex tasks. That’s what this book is about, so let’s dive in…

Book Structure and Contents

Foundations of Deep Reinforcement Learning - Theory and Practice in Python begins with a brief preliminary chapter, which serves to introduce a few concepts and terms that will be used throughout all the other chapters: agent, state, action, objective, reward, reinforcement, policy, value function, model, trajectory, transition.

Graph BFS With Different DS in Adjacency Matrix and Their Usability

This article analyzes the adjacency matrix used for storing node-link information in an array. For a Graph BFS (Breadth-first-search) traversal, we normally tend to keep an adjacency matrix as a 2D array (adj[][]) or array of linkedLists as LinkedList[]. As a result, if an element X is added before element Y, it appears in BFS traversal accordingly. BFS traversal becomes like: ~X,Y,~. What if, the appearance of elements within a breath of a graph needs to be traversed  according to some priority or any rule!!

Say in a breath if Priority of Y > Priority of X, element Y should occur before X in BFS traversal. Say In a breath if Y is added later the X, Y should occur before X in traversal.

Cryptography With the DES Algorithm

Nowadays, information security is the main concern on the Internet. With web data continuously flowing from one end to another, to ensure data security, there are many procedures that must be implemented. Different organizations are working to find a more secure way to protect data.

There are also some key terms when it comes to information security — like confidentiality, integrity, availability, etc. Confidentiality means that only authorized users can gain access to sensitive data. Integrity confirms that data has not been modified by any mid-level person. Additionally, this means that the data reached the other user without changes or a breach. Lastly, availability means that data is available for any authorized user. There are many procedures that confirm data confidentiality, integrity, and availability. Almost all procedures use some type of encryption/decryption algorithm to keep data secure from middle attacks. And there are two kinds of security algorithms: symmetric algorithms (use the same secret key to encrypt/decrypt data) and asymmetric algorithms (use different secret keys to encrypt/decrypt data). The main goal of this article is to describe DES algorithm and how it secures data. 

Approximation Algorithms for Your Database

In an earlier blog post, I wrote about how breaking problems down into a MapReduce style approach can give you much better performance. We’ve seen that Citus is orders of magnitudes faster than single node databases when we’re able to parallelize the workload across all the cores in a cluster. And while count (*) and avg is easy to break into smaller parts, I immediately got the question what about count distinct or the top from a list or median.

Exact distinct count is admittedly harder to tackle in a large distributed setup because it requires a lot of data shuffling between nodes. Count distinct is indeed supported within Citus, but at times can be slow when dealing with especially larger datasets. Median across any moderate to large size dataset can become completely prohibitive for end users. Fortunately, for nearly all of these, there are approximation algorithms that provide close-enough answers and do so with impressive performance characteristics.

Cryptographic Hash Function Menagerie

Here's an oversimplified survey of cryptographic hash functions: everyone used to use MD5, now they use some variation on SHA.

There's some truth to that. MD5 was very popular and remains popular years after it was proven insecure. And now, variations on SHA like SHA1 and SHA256 are commonly used. But there are a lot more cryptographic hash functions in common use.

How Fast Can You Multiply Really Big Numbers? [Snippet]

How long does it take to multiply very large integers? Using the algorithm you learned in elementary school, it takes O(n²) operations to multiply two n digit numbers. But for large enough numbers it pays to carry out multiplication very differently, using FFTs.

If you’re multiplying integers with tens of thousands of decimal digits, the most efficient algorithm is the Schönhage-Strassen algorithm, which takes

10 Things Every Programmer and Software Engineer Should Know

If you have been programming for some time and looking to learn to program then you might be thinking about what makes a good programmer. What can a computer science graduate do to prepare for a career in software development and programming? The things expected of a junior developer are some of the common questions I receive from many students on Facebook and Emails who follows me. These are mostly college students who now have access to a wealth of information thanks to the internet and eager to learn things in advance to prepare for their programming job interviews.

In this article, I'll share 10 things which I believe every programmer should know. This includes a programming language like C++ or Java, essential computer science concepts like data structures, algorithms and computer networking basics, essential tools like Git, Microsoft Word and Excel, skills like SQL and UNIX, editors like Eclipse or Visual Studio, and text editors.