added Knuth–Morris–Pratt (KMP) Algorithm for Pattern Searching (#19323)

* added basic useful docker commands

* setting up webserver inside docker and creating an image

* Added KMP algorithm for pattern matching
This commit is contained in:
Adarsh Jain
2018-10-15 22:20:01 +05:30
committed by Todd Chaffee
parent 619f955faa
commit 16e20191fc
3 changed files with 255 additions and 0 deletions

View File

@ -0,0 +1,54 @@
# KnuthMorrisPratt Algorithm for Pattern Searching
Pattern searching is an important problem in computer science. When we do search for a string in notepad/word file or browser or database, pattern searching algorithms are used to show the search results.
**Problem :**
Given a text _txt[0..n-1]_ and a pattern _pat[0..m-1]_, write a function _search(char pat[], char txt[])_ that prints all occurrences of _pat[]_ in _txt[]_. You may assume that _n > m_.
**Example :**
```
Input: txt[] = "AABAACAADAABAABA"
pat[] = "AABA"
Output: Pattern found at index 0
Pattern found at index 9
Pattern found at index 12
```
**Idea :**
The basic idea behind KMPs algorithm is: whenever we detect a mismatch (after some matches), we already know some of the characters in the text of the next window. We take advantage of this information to avoid matching the characters that we know will anyway match. Let us consider below example to understand this.
**Preprocessing Pattern String :**
- KMP algorithm preprocesses pat[] and constructs an auxiliary **lps[]** of size m (same as size of pattern) which is used to skip characters while matching.
- Name lps indicates **longest proper prefix** which is also suffix. A proper prefix is prefix with whole string **not** allowed. For example, prefixes of “ABC” are “”, “A”, “AB” and “ABC”. Proper prefixes are “”, “A” and “AB”. Suffixes of the string are “”, “C”, “BC” and “ABC”.
- We search for lps in sub-patterns. More clearly we focus on sub-strings of patterns that are either prefix and suffix.
- For each sub-pattern pat[0..i] where i = 0 to m-1, lps[i] stores length of the maximum matching proper prefix which is also a suffix of the sub-pattern pat[0..i].
`lps[i] = the longest proper prefix of pat[0..i] which is also a suffix of pat[0..i]. `
**Note :** lps[i] could also be defined as longest prefix which is also proper suffix. We need to use properly at one place to make sure that the whole substring is not considered.
**Examples of lps[] construction :**
```
For the pattern “ABCDE”,
lps[] is [0, 0, 0, 0, 0]
For the pattern “AABAACAABAA”,
lps[] is [0, 1, 0, 1, 2, 0, 1, 2, 3, 4, 5]
```
### Searching Algorithm :
In this algorithm, we use a value from lps[] to decide the next characters to be matched. The idea is to not match a character that we know will anyway match.
How to use lps[] to decide next positions (or to know a number of characters to be skipped)?
- We start comparison of pat[j] with j = 0 with characters of current window of text.
- We keep matching characters txt[i] and pat[j] and keep incrementing i and j while pat[j] and txt[i] keep **matching**.
- When we see a **mismatch**
- We know that characters pat[0..j-1] match with txt[i-j+1…i-1] (Note that j starts with 0 and increment it only when there is a match).
- We also know (from above definition) that lps[j-1] is count of characters of pat[0…j-1] that are both proper prefix and suffix.
- From above two points, we can conclude that we do not need to match these lps[j-1] characters with txt[i-j…i-1] because we know that these characters will anyway match. Let us consider above example to understand this.
<br>
**More Infromation :**
- [kmp algorithm for pattern searching](https://www.geeksforgeeks.org/kmp-algorithm-for-pattern-searching/)
- [KnuthMorrisPratt algorithm](https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm)

View File

@ -0,0 +1,127 @@
# Useful commands for Docker
- Docker is mainly used for run programs on server side.
- Companies customize their OS before use. They don't require many things like GUI.
- Less program means less RAM used and more security.
- More features means more chances to hack, more vulnerabilities.
- We use OS to run program. Docker gives us an environment to run our program.
## Installing docker-engine
### For Redhat OS
- First, setup yum repo
```
[docker]
baseurl = https://yum.dockerproject.org/repo/main/centos/7
gpgcheck=0
```
- Then, install **docker-engine**
`$ yum install docker-engine`
### Start the services of docker
`$ systemctl restart docker`
It starts the docker server.
### See all the images available in docker
`$ docker images`
The default _docker images_ will show all top level images, their repository and tags, and their size.
### Load an image in docker
`$ docker load -i ubuntu-14.04.tar`
- **i** - Read from tar archive file, instead of STDIN
It loads an image or repository from a tar archive (even if compressed with gzip, bzip2, or xz) from a file or STDIN. It restores both images and tags.
### Docker run reference
- Docker runs processes in isolated containers.
- A container is a process which runs on a host. The host may be local or remote.
- When an operator executes `docker run`, the container process that runs is isolated in that it has its own file system, its own networking, and its own isolated process tree separate from the host.
### Run or start a new OS
`$ docker run -it ubuntu:14.04`
- The `docker run` command first `creates` a writeable container layer over the specified image, and then `starts` it using the specified command.
- The above example runs a container using the `ubuntu:14.04` image. The `-it` instructs Docker to allocate a pseudo-TTY connected to the containers stdin; creating an interactive `bash` shell in the container.
### See all the running OSs
`$ docker ps`
- The `docker ps` command only shows running containers by default.
- To see all containers, use the `-a` (or `--all`) flag:
`$ docker ps -a`
### Come out from docker OS console
`exit`
### From shell of docker OS, for coming out without exiting container
press _ctrl + p + q_
### From terminal of base system, to run a command in docker OS
`$ docker exec mycontainer ifconfig`
- **mycontainer** is the name of container.
- The `docker exec` command runs a new command in a running container.
### Usually run docker using this command
`$ docker run dit ubuntu:14.04`
- **i** - interactive
- **t** - terminal
- **d** - detach
### Stop all running OSs
```
$ docker ps -q //shows id of every running OS
$ docker stop $(docker ps -q)
```
### Permanently remove a container
`$ docker rm id`
### Permanently Remove all the stopped containers
`$ docker rm $(docker ps -a -q)`
- This command will delete all stopped containers.
- The command `docker ps -a -q` will return all existing container IDs and pass them to the `rm` command which will delete them.
- Any running containers will not be deleted.
### Remove containers while running (forcefully)
`$ docker rm -f $(docker ps -a -q)`
### Giving docker OS a name when starting
- By default, docker gives unique name to every container with a unique id.
- We can also give a name to container using following command -
`$ docker run -it --name adarsh centos:latest`
### Copy a file in container
`$ docker cp /root/form.txt myconatiner:/`
This command will copy a file form.txt from the base system to the specified container.
### Download docker images
[docker hub](http://hub.docker.com) - All the available docker images can be downloaded from this URL.
### Check different versions of OS that are available
```
$ docker search ubuntu //search
$ docker pull ubuntu:17.10 //downlaod required version
```
## Docker Storage
### Basic Storage types
1. **Empheral disk (temporary)** OS removal will remove data (like windows C drive)
2. **Persistent disk (permanent)** - OS removal will not erase data (like windows D drive)
- **-v** gives persistent storage. OS removal will not remove data.
### Docker volume manager
Docker by default takes space from **/** drive of host system to store data. Overall **/** drive amount of storage docker can use.
### Give separate space to a docker container
- Make a partition, format it and mount in base system.
Let the partition created is **mypart**
- Then, run following command
`$ docker run it -v /mypart:/data centos`
- **mypart** is a partition in base system and **data** is the folder where docker will store it's data.
- **v** - volume
### Attaching dvd to a container
`$ docker run it v /run/media/root/RHEL-7.3\ Server.. centos`
This command will attach a RHEL to the container.
### Copy content from a folder of base system to _/data_ in docker centos
`$ docker run it -v /folder_name:/data centos`

View File

@ -0,0 +1,74 @@
# Running Web Server inside Docker
### Start docker and its container -
```
$ systemctl restart docker
$ systemctl enable docker
$ docker run -it --name webserver centos:latest
```
### Install httpd
- **yum** is already configured in centos docker image.
So directly install **httpd** software -
`$ yum install httpd -y`
- Create a dummy web page to check the server -
```
$ cd /var/www/html
$ vi index.html
```
### Start services -
- If we use **systemctl** to start the services, this will not work and gives an error.
- **systemctl** doesn't work in docker.
- In actual RedHat system, when we start a service it actually runs a script in background. That script start daemons.
- To find the path of that script, check status of service
`$ systemctl status httpd`
`Loaded` option shows script file path.
- In that file, we have a line which actually starts service -
`ExecStart = /usr/sbin/httpd....... `
So the command `/usr/sbin/httpd` actually starts **httpd** server.
- Service is running or not, can be checked by -
`$ ps -aux | grep httpd`
- So we don't require `systemctl` we can directly start our web server using-
`$ /usr/sbin/httpd`
This will start the web server.
- `ifconfig` doesn't work in docker.
- So install software, which gives `ifconfig` command.
- It can be checked in actual Redhat system by running this command-
`$ rpm -qf /usr/sbin/ifconfig`
- This comes from **net-tools** package.
- So Install **net-tools** in docker os.
- Making _image_ of created web server -
`$ docker commit webserver apacheimg:v1`
- Name of container is _webserver_.
- This image can be share with exact setup with other users.
- To save created image -
`$ docker save apacheimg:v1 -o mywebserver.tar`
- To start **httpd** service automatically when container starts -
- Write `/usr/sbin/httpd` in following file.
`$ vim /root/.bashrc`
- To copy a file in container from the base system -
`$ docker cp /root/form.txt myconatiner:/`
## Summary
Summary of all the commands -
```
$ docker run -it --name webserver centos:latest
$ yum install httpd -y
$ rpm -q httpd
$ cd /var/www/html
$ vi index.html
// write some web content
$ /usr/sbin/httpd
$ yum install net-tool
$ ifconfig
```