Computer Science

6 posts

15 min read

Using the Proxy Pattern with QNetworkReply in Qt

qt design-patterns computer-science
Using the Proxy Pattern with QNetworkReply in Qt

The proxy pattern can separate network replies from the code that processes their data. This Qt tutorial compares a signals-and-slots approach with an APIReply wrapper around QNetworkReply.

Background: Processing Network Replies in Qt

In a previous project that I worked on, we had to connect to backend API using Qt framework in order to download some files and display some content of these files, this thing might look easily done using QNetworkAccessManager and QNetworkReply. But we had other things to consider:

  • Size of the downloaded files was large.
  • No file should be stored in the device running the application.
Read more →

18 min read

Stress Testing C++ Solutions for Competitive Programming

competitive-programming computer-science
Stress Testing C++ Solutions for Competitive Programming

Stress testing finds counterexamples by comparing a solution against a brute-force implementation on randomly generated inputs. This C++ tutorial covers test generators, comparison scripts, and custom checkers for problems with multiple valid answers.

I decided to write this article after watching the last screencast made by Errichto. To be more precise when he started making stress testing to find counter-test you can check that here. Errichto has a full video called How to test your solution in Competitive Programming, on Linux? I highly recommend you to check it, basically this article is the same but I am extending it to stress test solutions with multiple answers using a written checker, so let’s jump in.

Read more →

10 min read

Implementing Autocomplete in C++ with Tries and Ternary Search Trees

software-engineering computer-science
Implementing Autocomplete in C++ with Tries and Ternary Search Trees

Autocomplete relies on data structures that can find words by prefix efficiently. This article compares tries and ternary search trees and shows how to implement both approaches in C++.

How Prefix-Based Autocomplete Works

Autocomplete is the feature when an application predicts the complete word, after just typing some prefix of the word. You must have used autocomplete feature a lot in your life in many area like:

  • Search engines, Google search engine suggests to you an autocomplete feature when you type some text in the search bar, usually sorted by most trends.
  • Emails, as you start writing some prefix of the Email address you will get a list of suggestions.
  • Social media, like facebook, linkedin, instagram…
  • In source code editors, actually I am using Atom right now and I have used autocomplete for at least 10 times just in the first small part of this article.
Read more →

15 min read

Game Theory for Competitive Programming: Nim and Sprague-Grundy

competitive-programming computer-science
Game Theory for Competitive Programming: Nim and Sprague-Grundy

Combinatorial game theory helps determine whether a player can force a win. This tutorial introduces winning and losing positions, Nim, and the Sprague-Grundy theorem through competitive programming examples.

Combinatorial Games

The game is said to be Combinatorial if it satisfies the following conditions:

  1. The game is played between two players, we will call them Alice and Bob.
  2. The two players alternate playing, Alice plays first.
  3. We can define two sets \(F_1\) and \(F_2\) of possible moves for each player, these sets are usually finite, based on these two sets we can classify combinatorial games into:
    • Combinatorial impartial games this happens when \(F_1 = F_2\), this means both players have the same options of moving from each position.
    • Combinatorial partizan games this happens when \(F_1\ne F_2\), this includes games like chess or checkers in which one player moves the white pieces, and the other player moves the black pieces.
  4. There is an end position of the game, usually called the terminal position. In normal play rule the player making the last move wins the game, in misère play rules the player making the last move loses the game.
  5. The game always ends after some finite number of moves.
Read more →

15 min read

Maximum Independent Set in Bipartite Graphs

competitive-programming computer-science
Maximum Independent Set in Bipartite Graphs

This tutorial connects maximum independent sets, minimum vertex covers, and maximum matching in bipartite graphs. It explains the ideas and implements matching with max flow and augmenting paths in C++.

Introduction to Independent Sets

The title contains a lot of terms that should be explained separately before we start, given an undirected graph \(G = (V, E)\) we define the following terms:

  • Independent Set (IS) : Subset of nodes \(U \subseteq V\) with the following propriety: For any two nodes \(u, v \in U\) nodes \(u, v\) are not adjacent ( There is no direct edge between nodes \(u\) and \(v\) ).
  • Maximal Independent Set (MIS) : An independent set is maximal if no node can be added to it without violating the independence condition.
  • Maximum Independent Set (MaxIS) : An independent set of maximum cardinality.
Read more →

5 min read

Monotonic Queue: Sliding Window Minimum in C++

competitive-programming computer-science
Monotonic Queue: Sliding Window Minimum in C++

A monotonic queue finds the minimum in each sliding window in linear time. This tutorial develops a C++ implementation and extends it to minimums in two-dimensional submatrices.

Sliding Window Minimum: The Motivation Problem

Given \(n \times m\) matrix \((1 \le n, m \le 3*10^3)\) you have to calculate the sum of minimum numbers in all sub-matrices of size \(a\times b\) with top left corners in \((i, j)\) over all \(1 \le i \le n-a+1\) and \(1 \le j \le m-b+1\).

Read more →