38: Introduction to Graphs Chris Wyatt Electrical and Computer - - PowerPoint PPT Presentation

38 introduction to graphs
SMART_READER_LITE
LIVE PREVIEW

38: Introduction to Graphs Chris Wyatt Electrical and Computer - - PowerPoint PPT Presentation

ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs Chris Wyatt Electrical and Computer Engineering Virginia Tech Graphs One can approach graphs from different perspectives 1) It is a data structure: extension


slide-1
SLIDE 1

ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs

Chris Wyatt Electrical and Computer Engineering Virginia Tech

slide-2
SLIDE 2

Graphs

One can approach graphs from different perspectives 1) It is a data structure: extension of trees 2) Is is a mathematical construct 3) A model of many different real world problems

slide-3
SLIDE 3

From trees to graphs

Tree Graph

slide-4
SLIDE 4

Definition of a graph

A graph is a collection of vertices (nodes), V, and a set of pairs of vertices, E. G = {V,E} Example: V = {a b c d} E = {(a,c) (c,b) (b,d)

a b c d a b c d a and c are adjacent

slide-5
SLIDE 5

Terminology: Edges

Edges may be undirected

  • r

directed

  • rder of vertex pairs

neglected

  • rder of vertex pairs

determines direction

slide-6
SLIDE 6

Terminology: graphs vs multi-graphs

Graphs have at most one edge between two vertices V = {a b c d} E = {(a,c) (c,b) (b,d)} Multigraphs allow duplicate edges V = {a b c d} E = { (a,c) (c,b) (b,d) (a,c) }

a b c d a b c d

slide-7
SLIDE 7

Terminology: subgraphs

Any subset of V and E forms a subgraph

a b c f i a b i Undirected Graph G A subgraph of G

slide-8
SLIDE 8

Terminology: subgraphs

Any subset of V and E forms a subgraph

a b c f i Undirected Graph G Another subgraph of G b c f

slide-9
SLIDE 9

Terminology: paths

A path is a sequence of vertices connected by edges

a b c d e f g h i j k l Paths may be directed or undirected

slide-10
SLIDE 10

Terminology: paths

A path is a sequence of vertices connected by edges

a b c d e f g h i j k l Paths may be directed or undirected

slide-11
SLIDE 11

Terminology: cycle

A cycle is a path that starts and stops at the same vertex

a b c d e f g h i j k l

slide-12
SLIDE 12

Terminology: cycle

A cycle is a path that starts and stops at the same vertex

a b c d e f g h i j k l

slide-13
SLIDE 13

Terminology: cycle

A cycle is a path that starts and stops at the same vertex

a b c d e f g h i j k l

slide-14
SLIDE 14

Terminology: connected / disconnected

A graph is connected if every pair of vertices are connected by at least one path

a b c d f g i k l

slide-15
SLIDE 15

Terminology: connected / disconnected

Otherwise it is disconnected.

a b c d f g i k l

slide-16
SLIDE 16

Vertices and Edges can have properties or attributes attached to them.

Example: driving routes between cities

Blacksburg Radford Christiansburg Roanoke 7.86 miles 15 min 14.3 miles 23 min 10 miles 17 min 46.7 miles 53 min 37.5 miles 40 min

slide-17
SLIDE 17

Vertices and Edges can have properties or attributes attached to them.

Example: driving routes between cities

Blacksburg Christiansburg 7.86 miles 15 min Name GPS coord Population etc length in miles driving time road type (2 lane, 4 lane, access controlled)

slide-18
SLIDE 18

A commonly encountered graph is one where the edge property is a weight.

Weighted graphs (directed or undirected) have edges whose property is a cost. Often one want to find paths connecting nodes where some function of the sum of the weights is optimal a min or max). For example: what is the shortest route from Roanoke to Radford? what is the fastest? etc.

slide-19
SLIDE 19

Some categories of graphs

Complete Random Planar

slide-20
SLIDE 20

Small-World Graphs

Small world graphs often appear in the real world and have very interesting properties.

  • Seven degrees of Kevin Bacon
  • Large Scale Computer Networks
  • Brains

Neural Connections in the worm C-elegans 279 vertices 6,417 edges

slide-21
SLIDE 21

Example uses of graphs

Path planning Layout routing Games and puzzles Many kinds of circuits Networked systems Optimization Constraint Satisfaction Logical Inference Probabilistic Inference ..... on and on .....

slide-22
SLIDE 22

Implementing Graphs

There is no graph data structure in the current standard C++ library. It is easy to roll your own using existing standard library containers. There is also the boost graph library (www.boost.org)

slide-23
SLIDE 23

Three common approaches to representing graphs.

Adjacency matrix: given N vertices, the edges are indicated by an NxN matrix Adjacency List: given N vertices, the edges are indicated by a list of connected vertices for each vertex. Pointer based: given a pointer to a vertex, which contains pointers to it’s adjacent vertices

slide-24
SLIDE 24

Graph using an adjacency matrix

a b c d a 1 1 b 1 c 1 1 d a b c d

  • Undirected graphs have a symmetric matrix
  • Weighted graphs have integer or real entries
slide-25
SLIDE 25

Graph using an adjacency list

a b c d a b c d b d a b d

  • the lists could be vectors, linked, or trees
slide-26
SLIDE 26

Graph using pointers

  • graph must have a root and be connected.
  • Why?

a b c d root

slide-27
SLIDE 27

Advantages/Disadvantages of implementations

Adjacency matrix Advantages

  • 1. simple
  • 2. space efficient for dense graphs (~ complete)
  • 3. fast access to all edges

Disadvantages

  • 1. space inefficient for sparse graphs
slide-28
SLIDE 28

Advantages/Disadvantages of implementations

Adjacency list Advantages

  • 1. space efficient for sparse graphs

Disadvantages

  • 1. space inefficient for dense graphs
  • 2. access to arbitrary edges slower
slide-29
SLIDE 29

Advantages/Disadvantages of implementations

Pointer based Advantages

  • 1. space efficient for sparse graphs

Disadvantages

  • 1. space inefficient for dense graphs
  • 2. access to arbitrary edges slower
  • 3. cannot represent disconnected graphs (easily)
slide-30
SLIDE 30

Next Actions and Reminders

Read CH pp. 614-630 on graph traversals and algorithms Program 5 is due 12/11

Please Fill out the SPOT survey!