Download
# David Stotts Computer Science Department PowerPoint Presentation, PPT - DocSlides

mitsue-stanley | 2019-03-15 | General
** Tags : **
david-stotts-computer-science-department-1687989
sparse graph
nodes edges
graph
sparse
edges
nodes
nyc
list
vertex
cost
cells
hops
matrix
graphs
space
dense
vertices
### Presentations text content in David Stotts Computer Science Department

Show

UNC Chapel . Hill. Data Structures . and Analysis. (COMP 410). Modeling with Graphs,. Implementing Graphs . (part . 2. ). Automaton drawn as labeled directed graph. Finite State Machine. Token. Represents current state. ID: 756747

- Views :
**1**

**Direct Link:**- Link:https://www.docslides.com/mitsue-stanley/david-stotts-computer-science-department-1687989
**Embed code:**

Download this presentation

DownloadNote - The PPT/PDF document "David Stotts Computer Science Departmen..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Slide1

A better approach for sparse graphs

David StottsComputer Science DepartmentUNC Chapel Hill

Data Structures

and Analysis

(COMP 410)

Slide2Modeling with Graphs,Implementing Graphs (part 2)

Slide3Automaton drawn as labeled directed graphFinite State MachineTokenRepresents current state

A

F

C

h

G

g

B

c

g

D

a

H

c

a

r

r

k

E

,

g

a

c

c

k

,

c

a

r

r

r

r

g h ,

a

r g h ,

g

a c

c

k ,

g

a c

c

c

c

c k ,

a r

r

g h

Simple abstract “computer”

Recognizes and generate strings (languages)

Used in compilers, keyword tokenizers

COMP 455

models of languages and computation

Slide4Automaton that is represented with bipartite graphPredicate Transition Netsis bipartite

p1

p3

p5

t1

p4

t2

p2

t3

t4

t5

t6

Token

Represents locus of activity

Slide5Example: air routes, road mapsModeling with GraphsWeighted edges

bost

nyc

dc

atl

char

chi

denv

seatt

sfr

la

dall

70

30

20

10

2

5

4

0

20

3

0

50

7

0

40

3

0

15

20

25

20

9

0

10

5

10

2

0

50

2

0

10

Slide6Weights can be “cost”miles, speed, fuel, time, total $$, wire/fiber, etc.Directed edgescost might be less one way than anotherjet stream, might be faster west than eastdifferent planes on different routesuploads copper, downloads satelliteAdd weights to get total cost of a route A to BAdd #hops to get other solutions #take off/landings = tire wear, maint hours, taxes, risk

Modeling

Slide7What is min cost NYC to LA? NYC to LA min hops? NYC –30> Chi –50> Denv –40>

LA

3 hops, cost is 120

NYC

–

5

>

DC

–

70

>

Denv

–

40> LA

3 hops, cost is 115 NYC –

30> Chi

–70> Seatt

–20>

SFr –10

> LA 4 hops, cost is 130

NYC –5

> DC

–10

> Char

–30

> Dall

–20

> Denv

–40

> LA

5 hops, cost is 105

Can I get from DC to any other city?

Is the map strongly connected?

If so, then

yes

Modeling

Slide8Adjacency Matrix (unweighted edges)For N nodes, we use an NxN array of Booleanif AM[a,b]==True then (a,b)∈E

Representing Graphs

A B C D

A

B

C

D

source

destination

T

T

T

T

A

C

B

D

F

F

F

F

F

F

F

F

F

F

F

T

Slide9A B C D

A

B

C

D

source

destination

Weighted Edges

Array elements can be integer, real, etc.

Representing Graphs

4

2

2

6

3

A

C

B

D

2

2

3

4

6

0

0

0

0

0

0

0

0

0

0

0

Slide10Problem is wasted spaceAdjacency MatrixO() space

If |E|≪

lots of wasted space

64 spaces, 55 not used

8

nodes

T

T

T

T

T

T

T

T

T

If

|E|≈

we call the graph

dense

Then adjacency matrix is space efficient

If |E|≈

we say graph is O(

) in space.

If

|E|≪

we call the graph

sparse

Then adjacency matrix is not space efficient

Sparse can be surprising, denser than it sounds

Slide11

Let’s model traffic in ManhattanSparse?Say we have 3000 intersections in Manhattan

Let V be intersections of streets

Let E be streets between V

Assume 2-way, use digraph

Graph seems complex… is it dense?

Each node has 4 edges out (

non side nodes

)

So

|E

| ≈

for large V,

|E|≪

Matrix has

cells, or

9,000,000

cells

Cells used ≈ 4*3000, or

12,000

cells used

This leaves

8,988,000

cells

not

used

Our traffic flow graph is NOT dense, it is

sparse

Slide12SummaryEasy to build and codeUsually wastes most of its spaceWhen a graph is sparse, finding adjacent nodes is expensive Given vertex v, find all vertices adjacent to it Go to row v in matrixO(|V|) operation to look at all columns in that rowOnly find a few vertices (graph is sparse)Manhattan example: O(3000) looks to find 4 vertices

Adjacency Matrix

T

T

T

T

T

T

T

T

T

Slide13Dense? Or Sparse?From Jzy3d plotting software website1538 nodes, 8032 edges(1538 nodes)^2 is 23654448032

edges ≪ 2365444

so…

Sparse

Slide14Complete Graphs are Densefrom numerics.io Effluviamagazine.com/effluviablogFriday, June 27, 2014

K16

K100

Slide15Dense? Or Sparse?from Martin Grandjean on Digital Humanities site

Slide16A better approach for sparse graphs

Keep list of verticesEach vertex has a list of adjacent verticesList of listsHashMap of listsArray of listsAdjacency List

A

C

B

D

A

B

D

C

B

C

D

C

B

Slide17If edges are weighted store that in the link cellAdjacency List

A

C

B

D

2

2

3

4

6

A

B

C

D

2

4

3

6

2

Slide18Array for nodes, linked cells for edgesAnother Example1

A

C

B

D

3

5

3

1

2

E

6

4

A

B

D

C

0

1

2

3

4

E

2

1

3

4

3

2

1

2

3

5

3

1

6

4

0

1

Slide19Space needed: O( |V|+|E| )We call this “linear” for graphsSo “size” of a graph is |V|+|E|Find all vertices adjacent to some node v ?Worst case O(|V|) (complete graph)Normally O( adj list for v )Adj Matrix gives O(|V|) every search, worst and avgAdjacency List

Slide20You will need a hashmap for verticesHash each vertex name to the vertex objectMany graph algorithms will be inefficient withoutIf you need to find a vertex, you cannot afford to do O(|V|) search through an “all vertices” list without making many algorithms become quadratic, or worseYou will still want the vertices linked, for times you need to visit every vertex systematicallyMay need similar hash structure for edgesEfficiency

Slide21END

Next Slides