Skip to content

UKSPC Practice Problems » Identifying Concurrent Events

It is important in distributed computer systems to identify those events (at identifiable points in time) that are concurrent, or not related to each other in time. A group of concurrent events may sometimes attempt to simultaneously use the same resource, and this could cause problems.

Events that are not concurrent can be ordered in time. For example, if event e1 can be shown to always precede event e2 in time, then e1 and e2 are obviously not concurrent. Notationally, we indicate that e1 precedes e2 by writing e1→e2. Note that the precedes relation is transitive, as expected. Thus if e1→e2 and e2→e3, then we can also note that e1→e3.

Sequential events in a single computation are not concurrent. For example, if a particular computation performs the operations identified by events e1, e2 and e3 in that order, then clearly and e1→e2 and e2→e3.

Computations in a distributed system communicate by sending messages. If event esend corresponds to the sending of a message by one computation, and event erecv corresponds to the reception of that message by a different computation, then we can always note that esenderecv, since a message cannot be received before it is sent.

In this problem you will be supplied with lists of sequential events for an arbitrary number of computations, and the identification of an arbitrary number of messages sent between these computations. Your task is to identify those pairs of events that are concurrent.

Input and Output Specification

A number of test cases will be supplied. For each test case the input will include first an integer, NC, specifying the number of computations in the test case. For each of these NC computations there will be a single line containing an integer NEi that specifies the number of sequential events in the computation followed by NEi event names. Event names will always contain one to five alphanumeric characters, and will be separated from each other by at least one blank. Following the specification of the events in the last computation there will be a line with a single integer, NM, that specifies the number of messages that are sent between computations. Finally, on each of the following NM lines there will be a pair of event names specifying the name of the event associated with the sending of a message, and the event associated with the reception of the message. These names will have previously appeared in the lists of events associated with computations, and will be separated by at least one blank. The last test case will be followed by the single integer 0 on a line by itself.

For each test case, print the test case number (they are numbered sequentially starting with 1), the number of pairs of concurrent events for the test case, and any two pair of the concurrent event names. If there is only one concurrent pair of events, just print it. And if there are no concurrent events, then state that fact.

Example

Consider the following input data:
2
2 e1 e2
2 e3 e4
1
e3 e1
0
There are two computations. In the first e1->e2 and in the second e3->e4. A single message is sent from e3 to e1, which means e3->e1. Using the transitive property of the precedes relation we can additionally determine that e3->e2. This leaves the pairs (e1,e4) and (e2,e4) as concurrent events.

Sample Solution

/*
   ACM North Central Region, 1994-95
   Problem D, Identifying Concurrent Events

   Ed Karrels, May 1996
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node {
   int idx;
   char *name;
   struct Node *next, *child, *sibling;
} Node;

Node *head=0, *tail=0;
int n_nodes = 0;

Node *FindNode(char *str) {
   Node *p;
   p = head;
   while (p && strcmp(p->name, str)) p = p->next;
   if (!p) {
      fprintf(stderr, "name '%s' not found.\n", str);
      exit(1);
   }
   return p;
}

Node *IndNode(int i) {
   Node *p;
   p = head;
   while (i--) p = p->next;
   return p;
}


void FreeNodes() {
   Node *node;
   node = head;
   while (node) {
      head = node->next;
      free((char*)node);
      node = head;
   }
   head = tail = 0;
   n_nodes = 0;
}

void AddChild(Node *par, Node *child) {
   Node *node;
   if (par->child) {
      node = par->child;
      while (node->sibling) node = node->sibling;
      node->sibling = child;
   } else {
      par->child = child;
   }
}

Node *NewNode(char *name, Node *parent) {
   Node *node;

   node = (Node*)malloc(sizeof(Node));
   node->idx = n_nodes++;
   node->name = strdup(name);
   if (parent) AddChild(parent, node);
   if (tail) {
      tail->next = node;
      tail = tail->next;
   } else {
      head = tail = node;
   }
   node->next = node->child = node->sibling = 0;
   
   return node;
}

#define REL(i, j) rel[i*n_nodes + j]

void MarkKids(Node *node, Node *child, int *rel) {
   if (child) {
      REL(node->idx, child->idx) = 1;
      REL(child->idx, node->idx) = 1;
      MarkKids(node, child->child, rel);
      MarkKids(node, child->sibling, rel);
   }
}

void Print(int *rel) {
   Node *node;
   int i, j;

   node = head;
   while (node) {
      printf("%2d: %s, next=%s, child=%s, sibling=%s\n",
	     node->idx, node->name, node->next ? node->next->name : "<>",
	     node->child ? node->child->name : "<>",
	     node->sibling ? node->sibling->name : "<>");
      node = node->next;
   }
   
   for (i=0; i<n_nodes; i++) {
      for (j=0; j<n_nodes; j++) {
	 printf("%d ", REL(i, j));
      } putchar('\n');
   }

   putchar('\n');
}

int main() {
   int a, b, c=1, i, j, *rel;  /* rel == 'related' */
   char str[100];
   Node *node1, *node2;

   scanf("%d", &a);
   while (a) {
      for (i=0; i<a; i++) {
	 scanf("%d", &b);
	 node1 = 0;
	 for (j=0; j<b; j++) {
	    scanf("%s", str);
	    node1 = NewNode(str, node1);
	 }
      }

      rel = (int*)malloc(sizeof(int) * n_nodes * n_nodes);
      for (i=0; i<n_nodes; i++)
	 for (j=0; j<n_nodes; j++)
	    REL(i, j) = 0;
      
      scanf("%d", &b);
      for (j=0; j<b; j++) {
	 scanf("%s", str);
	 node1 = FindNode(str);
	 scanf("%s", str);
	 node2 = FindNode(str);
	 /* printf("message from %s to %s\n", node1->name, node2->name); */
	 AddChild(node1, node2);
      }

      node1 = head;
      while (node1) {
	 MarkKids(node1, node1->child, rel);
	 node1 = node1->next;
      }

      /* Print(rel); */
      
      b = 0;
      for (i=0; i<n_nodes; i++) {
	 for (j=i+1; j<n_nodes; j++) {
	    if (!REL(i, j)) {
	       /* printf(" (%s, %s)\n", IndNode(i)->name, IndNode(j)->name); */
	       b++;
	    }
	 }
      }
      if (b) {
	 printf("Case %d, %d concurrent events: \n", c++, b);
	 if (b>2) b=2;
	 for (i=0; i<n_nodes && b; i++) {
	    for (j=i+1; j<n_nodes && b; j++) {
	       if (!REL(i, j)) {
		  printf("(%s,%s) ", IndNode(i)->name, IndNode(j)->name);
		  b--;
	       }
	    }
	 }
	 putchar('\n');
      } else {
	 printf("Case %d, no concurrent events.\n", c++);
      }

      FreeNodes();
      free((char*)rel);

      scanf("%d", &a);
   }

   return 0;
}

Sample Input

2
2 e1 e2
2 e3 e4
1
e3 e1
3
3 one two three
2 four five
3 six seven eight
2
one four
five six
1
3 x y zee
0
2
2 alpha beta
1 gamma
1
gamma beta
0

Sample Output

Case 1, 2 concurrent events: 
(e1,e4) (e2,e4) 
Case 2, 10 concurrent events: 
(two,four) (two,five) 
Case 3, no concurrent events.
Case 4, 1 concurrent events: 
(alpha,gamma)