Docsity

CSE373: Data Structures & Algorithms Lecture 15, Slides of Algorithms and Programming

Partial preview of the text

Download CSE373: Data Structures & Algorithms Lecture 15 and more Slides Algorithms and Programming in PDF only on Docsity!

CSE373: Data Structures & Algorithms

Lecture 15: Introduction to Graphs

Nicki Dell

Spring 2014

Announcements

• Homework 4 is out

– Implementing hash tables and hash functions

– Due Wednesday May 14

th

at 11pm

– Allowed to work with a partner

• Midterm next Friday in-class

Graphs

  • A graph is a formalism for representing relationships among items
    • Very general definition because very general concept
  • A graph is a pair G = (V,E) - A set of vertices, also known as nodes V = {v 1 ,v 2 ,…,v n
  • A set of edges E = {e 1 ,e 2 ,…,e m
  • Each edge e i is a pair of vertices (v j ,v k
  • An edge “connects” the vertices
  • Graphs can be directed or undirected Han Leia Luke V = {Han,Leia,Luke} E = {(Luke,Leia), (Han,Leia), (Leia,Han)}

Undirected Graphs

  • In undirected graphs, edges have no specific direction
    • Edges are always “two-way”
  • Thus, (u,v) ∈ E implies (v,u) ∈ E
    • Only one of these edges needs to be in the set
    • The other is implicit, so normalize how you check for it
  • Degree of a vertex: number of edges containing that vertex
    • Put another way: the number of adjacent vertices

A

B

C

D

Self-Edges, Connectedness

  • A self-edge a.k.a. a loop is an edge of the form (u,u)
    • Depending on the use/algorithm, a graph may have:
      • No self edges
      • Some self edges
      • All self edges (often therefore implicit, but we will be explicit)
  • A node can have a degree / in-degree / out-degree of zero
  • A graph does not have to be connected
    • Even if every node has non-zero degree

More notation

For a graph G = (V,E) :

  • |V| is the number of vertices
  • |E| is the number of edges
    • Minimum?
    • Maximum for undirected?
    • Maximum for directed?
  • If (u,v) ∈ E
    • Then v is a neighbor of u , i.e., v is adjacent to u
    • Order matters for directed edges
      • u is not adjacent to v unless (v,u) ∈ E

A

B

C

D

V = {A, B, C, D}

E = {(C, B),

(A, B),

(B, A)

(C, D)}

|V||V+1|/2 ∈ O (|V|

2

|V|

2

∈ O (|V|

2

(assuming self-edges allowed, else subtract |V|)

Weighted Graphs

  • In a weighed graph, each edge has a weight a.k.a. cost
    • Typically numeric (most examples use ints)
    • Orthogonal to whether graph is directed
    • Some graphs allow negative weights ; many do not 20 30 35 60 Mukilteo Edmonds Seattle Bremerton Bainbridge Kingston Clinton

Examples

What, if anything, might weights represent for each of these? Do negative weights make sense?

  • Web pages with links
  • Facebook friends
  • Methods in a program that call each other
  • Road maps (e.g., Google maps)
  • Airline routes
  • Family trees
  • Course pre-requisites

Path Length and Cost

  • Path length: Number of edges in a path
  • Path cost: Sum of weights of edges in a path Example where P= [Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle] Chicago Seattle San Francisco Dallas Salt Lake City

length(P) =

cost(P) =

Simple Paths and Cycles

  • A simple path repeats no vertices, except the first might be the last [Seattle, Salt Lake City, San Francisco, Dallas] [Seattle, Salt Lake City, San Francisco, Dallas, Seattle]
  • Recall, a cycle is a path that ends where it begins [Seattle, Salt Lake City, San Francisco, Dallas, Seattle] [Seattle, Salt Lake City, Seattle, Dallas, Seattle]
  • A simple cycle is a cycle and a simple path [Seattle, Salt Lake City, San Francisco, Dallas, Seattle]

Undirected-Graph Connectivity

  • An undirected graph is connected if for all pairs of vertices u,v , there exists a path from u to v
  • An undirected graph is complete, a.k.a. fully connected if for all pairs of vertices u,v , there exists an edge from u to v Connected graph Disconnected graph plus self edges

Directed-Graph Connectivity

  • A directed graph is strongly connected if there is a path from every vertex to every other vertex
  • A directed graph is weakly connected if there is a path from every vertex to every other vertex ignoring direction of edges
  • A complete a.k.a. fully connected directed graph has an edge from every vertex to every other vertex plus self edges

Rooted Trees

  • We are more accustomed to rooted trees where:
    • We identify a unique root
    • We think of edges as directed: parent to children
  • Given a tree, picking a root gives a unique rooted tree
    • The tree is just drawn differently A

B

D E

C

F

G H

redrawn

A

B

D E

C

F

G H

Rooted Trees

  • We are more accustomed to rooted trees where:
    • We identify a unique root
    • We think of edges as directed: parent to children
  • Given a tree, picking a root gives a unique rooted tree
    • The tree is just drawn differently A

B

D E

C

F

G H

redrawn

F

G H C

A

B

D E