Sunday, July 24, 2016

Algorithms: Asymptotic Notations Explained Simply



This is to explain what is big O notation in simple terms. Most students understand O(n) and O(1), its little difficult to understand O(log n). I have tried my best to explain it as simple as possible. Hope this is useful.

What is algorithm?
Algorithms are methods or ways used to complete a certain operation or solve a problem. We all know that there is more than one way to solve a problem, similarly there is more than one algorithm to solve a problem.

Here comes a scenario, if there are more than one algorithm to solve a problem how can we represent which is better or more efficient?

In order to represent the efficiency of an algorithm Big O notations (such as O(n), O(1), O(log n) are used.
Common Big O notations,
O(n) – linear time operation
O(1) – constant time operation
O(log n) – logarithmic time operation

In order to understand Big O notation, we need to understand what is constant time operation, linear time operation and logarithmic time operation.

O(n) – Linear time operation:

Problem to be solved: Assume, we have a box which contains numbers or cards with numbers printed on them (like 1, 2, 3, 4, … 16) and we are asked find if number 6 is there in the box. What do we need to do?

Pick 1 number at a time and check if the number we picked is 6 (the number to search).

If the number we picked matches the number to be searched, then we are good else we need to pick another number from the box.  This way of picking one number at a time and verifying if it matches one after another till all the n numbers are picked is called linear operation. This way of searching n numbers is represented as O(n), if the number to find is the last number or worst case scenario we need to pick all the n numbers.

int n = 16;
int[] numbers = {11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10};
int numberToSearch = 16;

int find(int numberToSearch, int[] numbers) {
for (int i = 0; i < n; i++) {
     if (numbers[i] = numberToSearch)
          return i; //FOUND
}

return -1; //NOT FOUND
}

O(1) – Constant time operation:

Problem to be solved: Assume, we are given a box of numbers (1, 2, 3, 4, … 16) and outside the box its printed as the box contains 16 numbers. You are asked how many numbers/items are there in a box?

Because you know the box is labelled as it contains 16 balls, you reply saying box contains 16. If there another person asking you the same question next day, you can answer again just by looking at the box. Even if you get another box with 100 numbers in it and if it has a label saying the box contains 100 numbers. This is called constant time operation. It is represented as O(1).

int n = 16;
int[] numbers = {11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10};

int findSize(int[] numbers) {
     return numbers.length;
}

O(log n) – Logarithmic time operation:

Problem to be solved: Assume we have a box which contains numbers (1, 2, 3, 4, … 16) and all the numbers are in order. You are asked to find a number 5 in the box.

The catch here is, the numbers are in order. Let us split the numbers into two parts. Total of 16 numbers is divided into 2 sets each containing 8 numbers.

int n = 16;
int[] numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int numberToSearch = 16;

int median = 16/2 = 8;
int[] split1 = {1,2,3,4,5,6,7,8};
int[] split2 = {9,10,11,12,13,14,15,16};

Number 16 is greater than the greatest element in split, hence the number to search will only be in split2, continue this splitting until end of numbers or the number to search is found.


Thus,



Looking at the above picture, we are able to find the number in just 4 steps. This can be written as,
If we have 16 number, then number of steps required to find a number is √16 = 4,
This can be written as, 16 = 42
This can be written as log2 16 = 4,
This can be written as log2 n, or simply O(log n),
Thus, 4 steps are required to find a box containing 16 numbers splitting the box into 2.
 
int find(int[] numbers, int numberToSearch) {
        int left = 0;
        int right = a.length - 1;
       
       while (left <= right) {
          int mid = left + (right - left) / 2;
            
          if (numberToSearch < numbers[mid]){
              right = mid - 1;
          }
          else if (numberToSearch > numbers[mid]) { 
              left = right + 1;
          }
          else {
              return mid; //NUMBER FOUND
          }
        }
        return -1; //NUMBER NOT FOUND
 }


Note: Please feel free to comment if something is incorrect or can be made better considering the fact that this can be understood by some beginner.

Tuesday, May 3, 2011

JMS Messege Bridge between two Weblogic Servers


Everyday we look for information in the web, I hope everyone gets something which is of help to you to proceed to the next step. There might be something you can share which can helpful for others to proceed to next step. Hence please contribute to the web (blogs, forums etc.)


JMS Messege Bridge Setup steps


Scope: Between two different versions of Weblogic Server.

Steps to setup Messege bridge between Weblogic Server 9.2.2 and Weblogic Server 10.3.4

Source Queue: Weblogic Server 9.2.2
Target Queue: Weblogic Server 10.3.4

Note: The names in source server should not be there in target server. For example, the connectionFactory name, queue name, even the server name where the JMSServer is targeted should not be same.

Steps to setup JMSServer, Connection Factory and Queue in WLS 9.2.2:
1. Create or use a Weblogic Server 9.2.2 domain with the AdminServer name as AdminServer1 running on port 7001. Assuming we have only admin server and this is for testing purpose.

2. Login in to Weblogic Server 9.2.2 server console and Create a JMS Server and name it as JMSServer01 with the required persistent store and target it to AdminServer1.

3. Create JMS Module and name it as JMSSystemModule01 and target it to JMSServer01

4. Add a ConnectionFactory resource under JMSSystemModule01 with JNDI name “jms/ConnectionFactory01 and the name as ConnectionFactory01”

5. Add a Queue resource under JMSSystemModule01 with JNDI name “jms/Queue01” and the name as Queue01.

Steps to setup JMSServer, Connection Factory and Queue in WLS 10.3.4:

1. Create or use a Weblogic Server 10.3.4 domain with the AdminServer name as AdminServer running on port 8001. Assuming we have only admin server and this is for testing purpose.

2. Login to Weblogic Server 10.3.4 server console and Create a JMS Server and name it as JMSServer02 with the required persistent store and target it to AdminServer.

3. Create JMS Module and name it as JMSSystemModule02 and target it to JMSServer02

4. Add a ConnectionFactory resource under JMSSystemModule02 with JNDI name “jms/ConnectionFactory02” and the name as ConnectionFactory02.

5. Add a Queue resource under JMSSystemModule02 with JNDI name “jms/Queue02” and the name as Queue02.

Note: Its advised to setup bridging in higer version server.

Steps to setup Bridging in higer version server, in our case it is WLS 10.3.4

6. Login to weblogic 10.3.4 Server console and navigate to Services --> Messaging -->Bridges --> JMS Bridge Destinations

7. Name: SourceQueueBridge

8. Adapter JNDI name: eis.jms.WLConnectionFactoryJNDINoTX

9. Do not enter anything in Adapter classpath

10. Connection URL as http://<server-ip>:<7001>

11. Initial Context Factory:

12. Connection Factory JNDI name: jms/ConnectionFactory01

13. As we want to bridge two queues, one in WLS 9.2.2 and WLS 10.3.4, our destination is Queue and the JNDI name we need to enter is the queuename of the source.

Destination JNDI Name: jms/Queue01

14. Destination Type: Queue

15. User Name: weblogic

16. Password: <server password>

17. Confirm Password: <server password>

Creation of Source JMS Bridge is over, now we will create a bridge for target

18. Navigate to Services --> Messaging --> Bridges --> JMS Bridge Destinations

19. Name: TargetQueueBridge

20. Adapter JNDI name: eis.jms.WLConnectionFactoryJNDINoTX

21. Do not enter anything in Adapter classpath

22. Connection URL as http://<server-ip>:<8001>

23. Initial Context Factory: <no change>

24. Connection Factory JNDI name: jms/ConnectionFactory02

25. As we want to bridge two queues, one in WLS 9.2.2 and WLS 10.3.4, our destination is Queue and the JNDI name we need to enter is the queuename of the target.

Destination JNDI Name: jms/Queue02

26. Destination Type: Queue

27. User Name: weblogic

28. Password: <server password>

29. Confirm Password: <server password>

JMS Bridge Destinations to transfer messages from Source WLS922 server to target WLS1034 server are created. Now we combine the source and target JMS bridge destinations.

30. Navigate to Services --> Messaging --> Bridges --> New, to create a new Bridge

31. Source Bridge Destination: SourceQueueBridge

32. Target Bridge Destination: TargetQueueBridge

33. Leave the other fields with default values as of now.

34. Save and release the configuration.

35. Restart both the server. Restart of server where bridge setup is done alone is enough. This is just to avoid any development environment issues.

To Test the bridge:
Create a servlet to send data to “jms/Queue01”
Create a MDB to listen to “jms/Queue02”
Send a message and check the bridging.

Reference:
Configuring and Managing the Messaging Bridge- Configuring and Managing the Messaging Bridge

or http://download.oracle.com/docs/cd/E17904_01/wls.htm