Announcement New exam dates: Graphs Exam 1 Monday, Oct 6 th - - PDF document

announcement
SMART_READER_LITE
LIVE PREVIEW

Announcement New exam dates: Graphs Exam 1 Monday, Oct 6 th - - PDF document

Announcement New exam dates: Graphs Exam 1 Monday, Oct 6 th Exam 2 Monday, Nov 3 rd Terminology & Implementation Announcement Announcement The ACM is looking for a few good Actuallyso is Google programmers


slide-1
SLIDE 1

Graphs

Terminology & Implementation

Announcement

  • New exam dates:

– Exam 1 – Monday, Oct 6th – Exam 2 – Monday, Nov 3rd

Announcement

  • The ACM is looking for a few good

programmers

– ACM Programming Contest

  • 3 Programmers per Team
  • Prelims – Sept 26th
  • If interested, contact Paul Tymann (ptt@cs.rit.edu)

by Wednesday.

  • Did I mention free food?

Announcement

  • Actually…so is Google
  • Google Code Jam

– 1st Round: Oct 1-Oct 15 – See

  • http://topcoder.com/pl/?&module=Static&d1=googl

e&d2=google_overview

Before we begin

  • Any questions?

Graphs

  • A graph consists

– Of a collection of points sometimes called vertices – Some pairs of points are connected by a line segment sometimes called an edge

  • Edges

– May have a direction associated with them in which case the graph is called a directed graph (digraph) – A graph that contains edges that do not have a direction associated with them is called an undirected graph

slide-2
SLIDE 2

Graphs

F E D C B A F E D C B A Graphs are usually drawn using points for vertices and lines for Edges, but a graph is defined independent of its representation

Graph edges

  • In addition to being directed or undirected,

edges can be weighted or un-weighted.

– A weighted edge has a value associated with it – The weight often measures the cost of using the edge to go from one node to another

  • A vertex may also have data associated with

it.

Formal Definition

  • A graph G = ( V, E ), consists of

– V, a set of vertices – E, a set of edges where each edge is a pair of distinct elements from V

Formal Definition – Edges

  • In an undirected graph

– each edge e = <v1, v2 > is an unordered pair of distinct vertices

  • In a directed graph

– each edge e = <v1, v2 > is an ordered pair of distinct vertices – <v1, v2 > ≠ < v2, v1 >

  • For weighted edges, weight can have a scalar

value, w, associated with it

– <v1, v2, w>

Formal definition example

  • V = { A, B, C, D, E,

F}

  • E = { <A, B>,
  • <B,C>,
  • <A,C>,
  • <A,D>,
  • <C,D>,
  • <E,F> }

F E D C B A

Formal definition example

  • V = { A, B, C, D, E,

F}

  • E = { <A, B, 5>,
  • <B,C, 20>,
  • <A,C, 15>,
  • <A,D, 10>,
  • <C,D, 25>,
  • <E,F, 30> }

F E D C B A 5 10 15 20 25 30

slide-3
SLIDE 3

Terminology

  • The degree of a vertex x is the number of

edges e in which x is one of the endpoints of edge e

  • The neighbors of a vertex v, are the vertices

that are directly connected to v

Terminology – Paths and Cycles

  • Two different vertices, x and y, in a graph

are said to be adjacent if an edge connects x to y

– I.e. if <x,y> is an edge, x and y are adjacent

Terminology – Paths and Cycles

  • A path is a sequence of vertices in which each

vertex is adjacent to the next one

– V1, V2, …, Vn and <Vi , Vi+1 > is an edge.

  • A simple path is a path in which no vertex is

repeated

  • A cycle is a path of length greater than one that

begins and ends at the same vertex

  • A Hamiltonian cycle is a cycle that visits all

vertices exactly once.

Paths and Cycles -- Example

  • ADCFDC is a path
  • ADC is a simple path
  • ADCA is a cycle
  • ADFECBA is a

Hamiltonian Cycle.

F E D C B A

Connectedness

  • A graph is connected if

– For every pair of nodes A, B, there is a simple path from A to B.

  • A subset of nodes that are connected define

a connected subgraph

Connectedness – Example

F E D C B A F E D C B A Not connected {A,B,C,D} and {E,F} form connected subgraphs Connected

slide-4
SLIDE 4

Terminology

  • Vertex, Edges (weighted and unweighted)
  • Directed vs. undirected
  • Paths and Cycles
  • Connectedness
  • Questions so far?

Building a Graph

import java.util.*; public interface DiGraph { // Methods to build the graph public void addVertex( Object key, Object data ); public void addEdge( Object fromKey, Object toKey, Object data ) throws NoSuchVertexException; ... } // DiGraph

Building a Graph

A

addVertex (“A”, “A”);

B

addVertex (“B”, “B”);

C

addVertex (“C”, “C”);

5

addEdge (“B”, “C”, new Integer(5));

2

addEdge (“A”, “B”, new Integer(2));

D

addVertex (“D”, “D”);

6

addEdge (“A”, “D”, new Integer(6));

7

addEdge (“B”, “D”, new Integer(7));

Graph Implementations

  • Adjacency Matrix
  • Adjacency List

Adjacency Matrix

  • Assumes vertices are numbered from 0 – n-

1.

  • Graph represented by a N x N, 2

dimensional array.

  • Array element (i, j) indicate in there is an

edge between vertex i and vertex j

Adjacency Matrix

  • For unweighted edges, array elements are

boolean.

0 1 2 3 4 0 F F T F T 1 F F F F F 2 T F F T T 3 F F T F T 4 T F T T F

2 1 3 4 T = edge F = no edge

slide-5
SLIDE 5

Adjacency Matrix

  • For weighted edges, array elements can

hold weights. 0 1 2 3 4 0 ∞ ∞ 1.0 ∞ 3.7 1 ∞ ∞ ∞ ∞ ∞ 2 1.0 ∞ ∞ 6.1 4.2 3 ∞ ∞ 6.1 ∞ 1.5 4 3.7 ∞ 4.2 1.5 ∞

2 1 3 4

6.1 4.2 3.7 1.0 1.5

∞ indicates no edge

Adjacency Matrix

  • Array elements might also contain objects
  • f an Edge class.

0 1 2 3 4

NULL NULL 1.0 NULL 3.7

1

NULL NULL NULL NULL NULL

2 1.0

NULL NULL 6.1 4.2

3

NULL NULL 6.1 NULL 1.5

4 3.7

NULL 4.2 1.5 NULL

2 1 3 4

6.1 4.2 3.7 1.0 1.5

NULL indicates no edge

Adjacency Matrix

  • Things to note:

– For an undirected graph, only half of the matrix is needed. – Lots of wasted space if not a lot of edges in the graph.

Adjacency List

  • Assumes vertices are numbered from 0 – n-

1.

  • Graph is represented by an array of lists,
  • ne list for each vertex.
  • Elements of list corresponding to a vertex

indicates edges to neighboring vertices.

Adjacency List

  • For unweighted edges, list nodes simply

give the neighboring vertex

2 1 3 4 1 2 3 4 2 4 3 4 2 4 3 2

Adjacency List

  • For weighted edges, list nodes give the

neighboring vertex and the weight

2 1 3 4

6.1 4.2 1.0 1.5

1 2 3 4

2 1.0 4 3.7 3 6.1 1.0 4 4.2 2 6.1 4 1.5 3 1.5 2 4.2 3.7

3.7

slide-6
SLIDE 6

Adjacency List

  • List elements might also contain objects of

an Edge class.

2 1 3 4

6.1 4.2 1.0 1.5

1 2 3 4

2 1.0 4 3.7 3 6.1 1.0 4 4.2 2 6.1 4 1.5 3 1.5 2 4.2 3.7

3.7

Graph Implementations

  • Graph Vertices

– Very often, other data may be associated with a vertex – It is often useful to create a Vertex class that holds all data. – Each graph may, in addition to edge representation, contain a set of Vertex class

  • bjects.

Graph Implementations

  • Comparison – space

– Graph of 50 Vertices – 4bytes / entry

1,200 10,000 100 2,500 2,000 1,500 1,200 1,000 500 250 49 Edges 20,400 10,000 16,400 10,000 12,400 10,000 10,000 10,000 8,400 10,000 4,400 10,000 2,400 10,000 792 10,000 Adj List

  • Adj. Matrix

Graph Implementations

  • Comparison Time

O (E / N) O (1) Delete Edge O (E / N) O (1) Insert Edge Find Neighbors Delete Node Insert Node Operation O (E / N) O(N) O (E) O (N) O (E / N) O (1) Adj List

  • Adj. Matrix

N = number of nodes E = number of edges E / N = avg number of edges per vertex

Graph Implementations

  • When implementing CS3DiGraph

– Choose implementation best suited for TSP – Feel free to use nested classes for aux classes (if required) – Feel free to use Java Collections Framework

Summary

  • Graphs
  • Terminology
  • Building a Graph
  • Implementing a Graph
  • Tomorrow: Graph Traversals
  • Questions?