Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

Есть алгоритм Флойда на языке python. Перепиши на языке R # Number of vertices nV = 4…

  1. User

    Есть алгоритм Флойда на языке python. Перепиши на языке R # Number of vertices nV = 4 INF = 999

    Algorithm

    def floyd(G): dist = list(map(lambda p: list(map(lambda q: q, p)), G))

    # Adding vertices individually
    for r in range(nV):
        for p in range(nV):
            for q in range(nV):
                dist[p][q] = min(dist[p][q], dist[p][r] + dist[r][q])
    sol(dist)
    

    Printing the output

    def sol(dist): for p in range(nV): for q in range(nV): if(dist[p][q] == INF): print("INF", end=" ") else: print(dist[p][q], end=" ") print(" ")

    G = [[0, 5, INF, INF], [50, 0, 15, 5], [30, INF, 0, 15], [15, INF, 5, 0]] floyd(G)

  2. Assistant

    nV <- 4 INF <- 999

    floyd <- function(G) { dist <- matrix(G, nrow=nV, ncol=nV, byrow=TRUE)

    for (r in 1:nV) { for (p in 1:nV) { for (q in 1:nV) { dist[p,q] <- min(dist[p,q], dist[p,r] + dist[r,q]) } } } sol(dist) }

    sol <- function(dist) { for (p in 1:nV) { for (q in 1:nV) { if (dist[p,q] == INF) { cat("INF", " ") } else { cat(dist[p,q], " ") } } cat("\n") } }

    G <- matrix(c(0,5,INF,INF,50,0,15,5,30,INF,0,15,15,INF,5,0), nrow=nV, ncol=nV, byrow=TRUE) floyd(G)