You are on page 1of 49

Topic: Trees and Graphs

Graphs (1)

TREES

Static

Dynamic

Spatial Trees
(Computer Graphics)

Game Trees

Search Trees

Priority Queues and Heaps

Graphs

Binary Search Trees


Self-Balancing

2-3 Trees

Huffman Tree

Other ()

AVL Trees

Red Black Trees

TREES

Static

Dynamic

Spatial Trees
(Computer Graphics)

Game Trees

Search Trees

Priority Queues and Heaps

Graphs

Binary Search Trees


Self-Balancing

2-3 Trees

Huffman Tree

Other ()

AVL Trees

Red Black Trees

What is a Graph?
A graph consists
Of a collection of points called vertices. Pairs of points are connected by a line segment called an edge.

What is a Graph?
A graph is made up of dots connected by lines A "dot" is called a vertex A "line" is called an edge

What is a Graph?
Graphs are usually drawn using points for vertices and lines for edges.

Undirected Graph

Directed Graph (digraph)

What is a Graph?
A graph G is an ordered pair of sets [V,E] where V = {v1, v2, ..., vi, ...,vn} is a set of vertices, E = {e1, e2, ..., ei, ..., em} is a set of edges. Data structure heavily used in computing problems.

Applications
Electronic circuits
start CS203

find the path of least resistance to CS203

Applications
Networks: (roads, flights, communications)

PVD LAX HNL DFW FTL STL

Applications
Scheduling (project planning)
wake up meditate work play assignment sports make cookies sleep dream eat

Go to class

Graph Problems and Modeling


Problem Solution

Graph Model

Known Method

Graph Solution

Graph Problems and Modeling


By reducing our problem to a well known problem, we can take advantage of known solutions for the problem Graph models are powerful and well studied Very useful set of tools for problem solving

Graph Model
Problem:

Shortest Path

You want to drive from Paris to Madrid. Given a road map of France/Spain on which the distance between each pair of adjacent intersections is marked, how do you determine the shortest route?

Graph Model
Problem:

Longest Path

In an automobile assembly plant the work is broken down into a number of tasks, for each of which the time needed is known. The physical circumstances impose certain ordering relations among the tasks, that is, some cannot be started until others have been completed. What is the minimum time needed to complete an assembly?
assuming that enough manpower is available to perform tasks in parallel whenever necessary.

Graph Model
Problem:

Traveling Salesman

A salesman has n customers, all in different towns; He wished to visit them all in turn, starting and ending his journey at the same town and not passing twice through an intermediate town. Knowing the distances between every pair of towns, in what order should he make the visits so as to travel the least possible total distance? This is a famous problem called the Traveling Salesman Problem.

Graph Model
5 1 2 1 4 3

Traveling Salesman

The edges are marked with distance (in miles). A solution is shown in red

Graph Model
Problem:

Vertex Cover

The police has a list of n gangsters. Some of the gangsters know each other. The police would like to listen to all the phone conversations between the gangsters who know each other. What is the minimum number of gangsters whose phones the police must tap?

Graph Model
Problem:

Dominating Set

There are n students in a class. Some of the students know each other. To get feedback about the course, the teaching staff want to talk to a subset of students. The subset must be selected so that every student knows (and is known by) someone in the subset. To save time,the teaching staff want to find the smallest such subset.

Graph Model
Problem:

Matching

You are given disjoint sets P of men and Q of women, and the pairs (p,q) that like each other. The marriage problem is to arrange as many marriages as possible with the restriction that married people should like each other.

Graph Model
Men Women

Matching

a maximum matching (in red)

Graph Model
Problem:

Spanning Tree

We would like to build a communication network linking n cities. We know the cost of linking each pair of cities. It is possible to link all cities together using n-1 links. Find a way to link together all the cities at the minimum cost.

Graph Model
Problem:

Spanning Tree

In designing electronic circuitry, we would like to make the N pins of several components electrically equivalent by wiring them together. We can use n-1 wires, each connecting two pins. Of all possible arrangements, the one that uses the least amount of wire is usually the most desirable.

What is a Graph?
A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V An edge e = (u,v) is a pair of vertices Example:

a c d

V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)}

Vertices
V represents the number of vertices in the graph V(G) is a set of vertices or nodes which can represent an object that needs to be connected

Edges
a distinct pair of vertices indicates a valid/existing connection between two vertices E the number of edges in the graph E(G) a set consisting of a pair of vertices
an edge which connects the same vertex is called a loop

Graphs
A C E

V=5 V(G) = {A,B,C,D,E} E(G) = {(AC), (AB), (BC), (CD), (CE), (DE), (DD)}

Size
The size of a graph is the number of vertices that it has.

Adjacent Vertices
if two vertices are joined by an edge they are said to be adjacent

Adjacent Vertices: A&C A&B B&C

Incident Vertices
if an edge connects two vertices, the vertices are said to be incident to the edge
A C

A is incident to edge AC B is incident to edge BA

Degree of a Vertex
The degree if a vertex v is the number of vertices adjacent to it (or the number of edges incident to it)
Represented as deg(v) or degree(v)

deg(A) = 1

deg(B) = 2

deg(C) = 1

Degree of a Vertex
The degree of a vertex in a graph is the number of edges that touch it. The number on each vertex of this graph is the degree of that vertex.

Regular Graphs
A graph is regular if every vertex has the same degree.

Paths
Given a graph G which includes the vertices x and y. A path from x to y length k is a sequence of k distinct edges e1, e2, .., en such that:
e1 = (x, x1), e2 = (x1, x2), ... e3 = (ek-1, y)

Path
A C E

Path from A to D: A-B-C-D Path from B to E: B-A-C-D-E

Cycle
If x and y are the same vertex, then we say that the path from x to y is a cycle
A C

Cycles: A-B-C-A C-B-A-C

Paths, Cycles, Circuit


A path is a route that you travel along edges and through vertices in a graph. All of the vertices and edges in a path are connected to one another. A cycle is a path which begins and ends on the same vertex. A cycle is sometimes called a circuit.

Path Length
The number of edges in a path or a cycle is called the length of the path.

Hamiltonian Path
A Hamiltonian path in a graph is a path that passes through every vertex in the graph exactly once. A Hamiltonian path does not necessarily pass through all the edges of the graph, however. A Hamiltonian path which ends in the same place in which it began is called a Hamiltonian circuit or a Hamiltonian cycle.

Eulerian Path
An eulerian path in a graph is a path that travels along every edge of the graph exactly once. An eulerian path might pass through individual vertices of the graph more than once. An eulerian path which begins and ends in the same place is called an eulerian circuit or an eulerian cycle

Connectivity
A graph G is connected if for every pair of vertices x and y, there exists a path from x to y
A B
Connected Graph

A B

Disconnected Graph

Distance in a Graph
Distance in a graph isn't measured in inches or kilometers. This isn't surprising, because you don't do any measuring in inches or kilometers when you draw a graph in the first place. Still, when you look at a graph, you can see how it might be possible to say that some vertices are closer together then others.

Distance
The distance d(x,y) from x to y is the smallest number of edges in a path from x to y
A C

d(A,C) = 1 d(B,C) = 1

Distance in a Graph
The distance between two vertices is a count of the number of edges along which you must travel to get from one of the vertices to the other. If there is more than one path between two vertices, the number of edges in the shortest path is the distance.

Isomorphic Graphs
Two graphs are isomorphic if you can re-draw one of them so that it looks exactly like the other. To re-draw a graph, it helps to imagine the edges as infinitely stretchable rubber bands. You can move the vertices around and stretch the edges any way you like -as long as they don't become disconnected.

Isomorphic Graphs
Sometimes it is very hard to tell whether two graphs are isomorphic or not. In fact, no one knows a simple method for taking two graphs and determining quickly whether or not they are isomorphic.

Complete Graphs
In a complete graph, every pair of vertices is connected by an edge. It is impossible to add an edge to a complete graph because every possible edge has been drawn.

You might also like