General Computer Science II (320201) Spring 2012 Assignment 1: Graph Theory

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

General Computer Science II (320201) Spring 2012 Assignment 1: Graph Theory

(Given Feb. 10., Due Feb. 16.)


20pt Problem 1.1 (Parse trees and isomorphism) Let Pe be the parse-tree of e := (x1 x2 + x2 x3 ) x4 1. Design a combinatorial circuit that represents Pe . Hint: Use the Dia diagram creation tool to draw the circuit. It runs both under Windows
and Linux. After installing it, you can use the Digital or the Logic add-ons, which contain ready AND, OR, NOT, etc. gates.

2. Write the mathematical representation of a graph G that is dierent but equivalent to Pe . 30pt Problem 1.2 (Tree Equivalences) Let G be a graph with v vertices. Prove that the following statements are equivalent: G is connected and it does not contain cycles. G contains no cycles, but adding one edge e will create exactly one cycle. Any two vertices of G are connected by strictly one path. 15pt Problem 1.3 (In-degrees in acyclic digraphs) Prove by induction that any acyclic digraph with non-empty set of nodes has at least one node with in-degree 0. msima 35pt Problem 1.4 (Graphs and SML) A common way to describe directed graphs is to list the direct neighbors of each node in a structure called adjacency list. In this problem, we will use a list of pairs (v, l), where l is the adjacency list of vertex v. For example, if we had node 1 connected to node 2 and 3, and 2 connected to 3 and 1, the list would look like: var adjList = [(1, [2, 3]), (2, [3, 1])]; In this example, 3 is not connected to any other vertex, so it does not appear in the adjacency list. Your tasks are: 1. Write an SML function getNeighbors which, given an graph represented with an adjacency list and a vertex v, returns the list of direct neighbors l corresponding to the vertex, or nil if the vertex has no neighbors.

2. Write an SML function isTree which, given a graph represented with an adjacency list, checks whether it is a tree. Remember that trees are directed acyclic graphs with a single node with in-degree 0 and all nodes but the root node has in-degree 1. 3. Write the SML functions size and depth which return the size and the depth of a tree represented with an adjacency list. Function signatures and example: val getNeighbors = fn : (int * int list) list * int -> int list; val isTree = fn : (int * int list) list -> bool; val size = fn : (int * int list) list -> int; val depth = fn : (int * int list) list -> int; - val adjList = [ (1, [2,3]), (2, [4, 5, 6, 7]), (5, [15, 12]) ]; - getNeighbors(adjList, 2); val it = [4, 5, 6, 7] : int list; - getNeighbors(adjList, 4); val it = [] : int list; - isTree(adjList); val it = true; - size(adjList); val it = 9: int; - depth(adjList); val it = 4;

You might also like